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 Jun 30, 2013 at 07:14 AM by Em3rgency for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by Em3rgency · Jun 29, 2013 at 01:29 PM · c#collisionnullreferenceexception

Yet another null reference exception

I have two identical instantiated objects in the game world. They both have the same script in them, and when they collide they both fire off the same error.

 void OnTriggerEnter(Collider other) 
     {
         colliderList.Add(other.gameObject);
 
     }
     void OnTriggerExit(Collider other)
     {
         colliderList.Remove(other.gameObject);
     }

NullReferenceException: Object reference not set to an instance of an object objectCollision.OnTriggerExit (UnityEngine.Collider other)

I need to track(and sometimes modify) each objects colliders, so what am I doing wrong here?

Comment
Add comment · Show 8
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 Benproductions1 · Jun 29, 2013 at 01:48 PM 2
Share

That depends on what colliderList is. It is probably null, therefore the null Reference Exception

avatar image Em3rgency · Jun 29, 2013 at 02:04 PM 0
Share

Well yes, it IS null, because it's initially empty. Hence the add/remove functions. The error specifies that (UnityEngine.Collider other)is what throwing the error so I don't think the list is the problem here.

Its obvious I'm referring to the colliding objects wrong. I assumed the collider in the enter/exit functions will return me the objects, but apparently that's not so...

avatar image Ray-Pendergraph · Jun 29, 2013 at 02:31 PM 0
Share

A null list is not the same as an empty list. An empty list is not null. Create your list.

avatar image Em3rgency · Jun 29, 2013 at 02:42 PM 0
Share

Ah. Looks like I need to redefine my conceptions of what null is. Ray, convert to answer please, your comment made me understand.

avatar image Eric5h5 · Jun 29, 2013 at 06:06 PM 0
Share

You don't accept answers by writing "solved" in the title, you use the check mark so they are actually marked as accepted.

Show more comments

1 Reply

  • Sort: 
avatar image
3
Best Answer

Answer by flamy · Jun 29, 2013 at 02:45 PM

Add this to the Start or Awake or OnEnable function of the class.

  colliderList =  new List<GameObject>();

This is needed because, an object needs to be initialized before being used.

and for OnTriggerExit()

 void OnTriggerExit(Collider other)
     {
        if(colliderList.Contains(other.gameObject))
             colliderList.Remove(other.gameObject);
     }

This needed because you might have removed the object from list elsewhere in the code or would do that in future. It is safe to check for it always.

Comment
Add comment · Show 6 · 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 Em3rgency · Jun 29, 2013 at 02:57 PM 0
Share

I already have the answer, but your tip gets you a thumbs up :) Thanks!

avatar image clunk47 · Jun 29, 2013 at 05:01 PM 0
Share

You should still mark this answer as accepted, so that people know this has been resolved. This does happen to be something you left out in your code, and this is why your code wasn't working. Especially considering you say you have the answer, but didn't post the answer, so this question remains open.

avatar image Em3rgency · Jun 29, 2013 at 05:08 PM 0
Share

Another person gave me the answer first in the comments above, I asked him to convert it into an answer so I can credit him properly. Read before posting.

avatar image clunk47 · Jun 29, 2013 at 05:24 PM 0
Share

Close the question if you don't want to accept the answer. I do read before posting. I read your question, all the comments, and this answer, which is legit. Accept the answer, or close your question.

avatar image Ray-Pendergraph · Jun 29, 2013 at 07:52 PM 2
Share

@Em3rgency, thanks for being thoughtful. Just please mark flamy's answer, $$anonymous$$e was not an answer it was a comment. The rules are the rules :-). Just glad to help.

Show more comments

Follow this Question

Answers Answers and Comments

20 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

Related Questions

OnCollision using Orthello2D Framework is causing NullReferenceException 1 Answer

Multiple Cars not working 1 Answer

Alternative collision detection methods? (c#) 1 Answer

Distribute terrain in zones 3 Answers

Trouble creating reference of image in script. 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