Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This question was closed Sep 06, 2015 at 03:05 PM by DrShikura for the following reason:

The question is answered, right answer was accepted

avatar image
1
Question by DrShikura · Jul 02, 2015 at 06:40 PM · raycastcollidertransformnullreferenceexceptionraycasthit

Finding a RaycastHit tag (Null Reference Exception)

So I've searched around for about 2 days and I've been unable to fix this issue I've been having. Wherein, I receive a Null Reference Exception when I try to check the tag of a raycast hit target on the ground. Here's a brief explanation of what I'm trying to do.

 var hit : RaycastHit;
 function Update() {
     Debug.DrawRay (transform.position, transform.forward * 10, Color.green);
     if (Physics.Raycast (transform.position, transform.forward, 10)) {
         if(hit.collider.tag == "Pickup"){
             print("Pickup!");
         }
         else{
             return hit;
         }
     }
 }

So essentially, my raycast is labeled here by the green Debug.DrawRay. The raycast shoots from the center of my flashlight beam and attempts to detect any items with the tag "Pickup" within a distance of 10 units from the player. However, when I attempt to ask about the tag using an if statement, I am uncertain what to use in order to ask the proper question. hit.collider.gameObject.tag or hit.collider.tag. I've also tried hit.transform.gameObject.tag and hit.transform.tag. All of which give me the same error. Which begs the question, am I missing something or am I going about this entirely wrong?


 NullReferenceException: Object reference not set to an instance of an object
 pickup_objects.Update () (at Assets/pickup_objects.js:4)

alt text

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

  • Sort: 
avatar image
2

Answer by kromenak · Jul 02, 2015 at 06:48 PM

I think the core issue is that you are never actually assigning the "hit" variable. You need to use one of the versions of Physics.Raycast that returns the RaycastHit variable when it hits something. If you don't do that, you'll always get a NullReferenceException, because your variable "hit" will always be null.

As for the correct variable to use from the RaycastHit class, "collider" or "transform" or "gameObject" would be fine.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image DrShikura · Jul 02, 2015 at 06:57 PM 0
Share

I got it! $$anonymous$$y issue was replacing the "10" in my raycast with "hit." I thought that location was solely for the length of the raycast, so I never thought to define the RaycastHit there. Thank you!

avatar image meat5000 ♦ · Jul 02, 2015 at 07:07 PM 0
Share

You need to read the raycast page in the scripting api. It shows how you can mix and match any arguments you need. You shouldnt need to replace anything, just add the extra info in, in the correct order for the given variation.

avatar image
0

Answer by Ibzy · Jul 02, 2015 at 06:54 PM

It looks like you're missing the "out hit" parameter from your raycast. Without it, your hit variable remains empty.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Follow this Question

Answers Answers and Comments

23 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

RaycastHit: What is the difference between hit.transform.tag and hit.collider.tag and which should I use when? 1 Answer

Difference between hit.collider.gameObject vs hit.transform.gameObject 2 Answers

RaycastHit: What is the difference between hit.transform.tag and hit.collider.tag? 1 Answer

Physics.Raycast not hitting anything 1 Answer

Raycast Collider Tag not returning correct result 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges