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 /
avatar image
0
Question by coolbird22 · May 23, 2014 at 06:39 PM · nullreferenceexception

Why am I getting this NullReferenceException error when everything seems to be in place ?

I need to spawn an explosion when my object collides with a gameobject under the tag 'Blast', for which I have the following code:

 void OnTriggerEnter2D(Collider2D blastCollisionCheck)
     {
         if (blastCollisionCheck.gameObject.tag == "Blast") {
             Spawn (gameObject.transform.position);
         }
     }

This spawn works alright, but whenever my object collides with a 'killer' gameobject (that just disables my current object) under a different tag, I start getting that NullReferenceException error. Anyone know why ?

Comment
Add comment · Show 2
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 rutter · May 23, 2014 at 07:12 PM 0
Share

Is this the code that's throwing the exception? If you look at the full exception text in the console window, you should see a line number and a stack trace.

avatar image coolbird22 · May 23, 2014 at 07:44 PM 0
Share

Yes. Here is the full exception text that I get. NullReferenceException: Object reference not set to an instance of an object CrowdBehavior.OnTriggerEnter2D (UnityEngine.Collider2D blastCollisionCheck) (at Assets/Scripts/CrowdBehavior.cs:42)

1 Reply

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

Answer by T27M · May 23, 2014 at 08:29 PM

So with the information in the question this answer is based on a lot of assumption, here is what I think is going on.

I'm assuming your "killer" gameobject does something like

     void OnTriggerEnter2D (Collider2D other)
     {
         other.gameObject.SetActive(false);
     }

It seems that when this happens the value stored variable called blastCollisionCheck is lost when the gameobject is disabled. The blastCollisionCheckis variable is now null.

That's where I think the NullReference is coming from. I'll leave it to you to figure out a work around.

Comment
Add comment · Show 4 · 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 coolbird22 · May 23, 2014 at 09:13 PM 0
Share

Yes, that is exactly what my 'killer' gameobject is doing. And I was right in assu$$anonymous$$g what you suggested that the blastCollisionCheck variable is lost when it is disabled and becomes null. I don't $$anonymous$$d that it becomes null and gets lost. How do I make sure that Unity doesn't keep flagging it as a NullReferenceException everytime this happens ? Also, is it a good habit to allow this to happen ?

avatar image T27M · May 23, 2014 at 09:38 PM 1
Share

You can check to make sure it is not null by surrounding your code with the following statement.

 if (blastCollisionCheck != null) {
    if (blastCollisionCheck.gameObject.tag == "Blast") {
      Spawn (gameObject.transform.position);
    }
 }

A null reference is bad, because as you have found out it will throw an error, may crash your program and/or lead to other unexpected things happening.

avatar image coolbird22 · May 23, 2014 at 09:45 PM 0
Share

That totally worked like a charm. I will keep a lookout on anything unexpected to be happening, though I doubt it should, since this is the only time that variable is ever used and I won't be accessing it ever again. Thanks for your help, @T27$$anonymous$$

avatar image T27M · May 23, 2014 at 09:48 PM 0
Share

The checking won't cause problems. I meant if you left the null reference in the program it would cause problems, like the one you were having.

Glad I could help ;)

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.

Follow this Question

Answers Answers and Comments

22 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

Related Questions

Null Reference Excepting :Object reference not set to an instance of object while changing text of 3D text 2 Answers

NullReference on textfield 1 Answer

Can I Ignore Null Reference Exceptions? ( bug? ) 3 Answers

itween example work well on unity3d while NullReferenceException on android 0 Answers

GameObject.Find/WithTag returns 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