• 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 /
avatar image
0
Question by MibZ · Sep 19, 2012 at 10:55 PM · nullreferenceexceptionontriggerexit

Checking if a variable is null gives a nullreference. Why?

EDIT: Before you stop reading and assume you know what I'm talking about, look at this screenshot. My question is not "which variable in this code is null?" so please stop answering that question.

My problem is that I only want the code inside of OnTriggerEnter/OnTriggerExit to compile if the intersecting collider is not null and only if it is a specific collider (attached to the player's hand, I have a reference to it (grabber) for comparison) so I have the lines 'if (other != null)' and 'if (other == grabber.collider)'...the NullReferenceException is thrown on the second line, but the problem is that the first line should have prevented it from even getting to that point. Why is it getting past that check and then throwing a NRE?

Original Post: I use a lot of OnTrigger functions in the project I'm working on, and occasionally OnTriggerExit throws nullreference exceptions, so I added a null check at the beginning of the function so it doesn't have any undesired behaviors.

Both if statements throw nullreferences when they reach 'other'. Why? I'm not attempting to do anything with the collider other than compare it to another collider.

The game runs fine and I haven't noticed any problems this has caused other than runtime null exceptions, but I would like to see them gone.

EDIT: The problem is not that 'grabber' is null, it is instantiated in OnTriggerEnter to store the colliding object and it is nullified at the end of OnTriggerExit. (I left out the code inside the nested if because it isn't problematic)

 private void OnTriggerExit(Collider other)
     {
         if (other != null)
         {
             if (other == grabber.collider)
             {
                  //Imagine there is code here
             }
         }
     }


Comment
Add comment · Show 6
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 whydoidoit · Sep 19, 2012 at 10:56 PM 1
Share

It's more likely that grabber is null and hence grabber.collider is an exception...

avatar image MibZ · Sep 19, 2012 at 11:14 PM 0
Share

Grabber is instantiated in OnTriggerEnter and isn't changed until OnTriggerExit, I'll add that to the main post.

avatar image whydoidoit · Sep 20, 2012 at 12:02 AM 0
Share

And grabber cannot have Destroy() called on it or one of it's parents? Nor can the collider? Just wondering as Destroy causes references to return null without being nullified.

avatar image Seth-Bergman · Sep 20, 2012 at 12:10 AM 1
Share

when you say grabber is instantiated in OnTriggerEnter, do you mean you are declaring it there? If so, it is out of scope..

avatar image aldonaletto · Sep 20, 2012 at 12:35 AM 1
Share

The only way you could have a Null Reference exception in this piece of code is grabber being null - this error says that a null variable is being used as a reference, and the only reference used in this piece of code is grabber. It must be null to cause this error, or the error is happening in another part of your code. In which line the error is hapenning? Show the entire relevant part of your script: the line where grabber is declared, where something is assigned to it, and the events OnTriggerEnter and OnTriggerExit.

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
4
Best Answer

Answer by skalev · Sep 19, 2012 at 11:58 PM

Something here is very weird, My suggestion is to put break points, or have the stop on error flag set in the editor, so you can see what is null. This should give you the insight you need, since from this piece of code, nothing should ever throw an exception. something else is happening. Another thing that I can think of, you said grabber is initialized onEnter, and released onExit. Could it be that another object is calling the same function, so that you have:
OnEnter
OnEnter
OnExit
OnExit -> grabber is null ?

Comment
Add comment · Show 11 · 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 Bunny83 · Sep 20, 2012 at 10:29 AM 1
Share

Good point! A typical "session" or "singleton" problem ;) There could be more than one collision at the same time.

avatar image save · Sep 20, 2012 at 10:43 AM 0
Share

Yup this happens a lot! $$anonymous$$eeping track of objects in an array that enter and exit is key here.

avatar image MibZ · Sep 20, 2012 at 03:03 PM 0
Share

Another little piece I forgot to mention since I've gotten used to it - Breakpoints in Unity haven't worked since the last update...I'm using Visual Studio Professional, attach the process to the Unity Editor, and can place breakpoints, but no matter where I put them the compiler thinks they're unreachable, even if they're at the beginning of a Start function or on a line of code that is clearly executing, like a Debug line.

avatar image whydoidoit · Sep 20, 2012 at 03:05 PM 0
Share

You should try debugging with $$anonymous$$onoDevelop then - or grab a copy of UnityVS - that's pretty hot for debugging from Visual Studio.

avatar image Bunny83 · Sep 20, 2012 at 10:05 PM 1
Share

Just to explain it one more time: grabber is a reference variable. It might hold a reference to a "grabber" object or it might be null. This is absolut valid.

But when grabber is null you can't access the collider property of the referenced object since it's null, there is no object. It's totally fine to compare the "reference value" of your grabber variable, but you can't use this reference to access anything from the object behind the variable because "there is no spoon (or grabber)".

I'm not sure if it's clear now, but i really can't explain it any more in detail ;)

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

16 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

Related Questions

NullReferenceException? 2 Answers

Null Reference Expantion 1 Answer

Null Reference Exception For Blocks C# 1 Answer

No idea what happend to unity (See pictures) 0 Answers

Physics2D.Linecast and Physics2D.Raycast don't seem to return null 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