Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Darn_ · May 03, 2017 at 08:42 AM · c#nullreferenceexceptioncollider2d

Adding and removing specific components to a list based OnTriggerEnter/Exit, Getting Null Exceptions

Here's the code in question:

 void OnTriggerEnter2D(Collider2D other)
 {
     if (other.gameObject.GetComponentInParent<Room>()) 
     {
         r = other.gameObject.GetComponentInParent<Room>();
         camGoto = true;
         if (!colList.Contains(other))
             colList.Add(other);
     }
 }
 void OnTriggerExit2D(Collider2D other)
 {
     if (other.gameObject.GetComponentInParent<Room>())
     {
         if (colList.Contains(other))
             colList.Remove(other);
     }
 }

Upon starting the game I get a bunch of "NullReferenceException: Object reference not set to an instance of an object PlayerController.OnTriggerEnter2D (UnityEngine.Collider2D other) (at Assets/Scripts/PlayerController.cs:63)" (presumably because those objects didn't get their parent set yet?). When I collide with something else (in my case another player, only without the controller script) I get those errors again, also for the Exit this time.

Any explanation why this is happening/how to fix it? Everything seems to work despite the errors but it doesn't feel right just letting them be.

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

1 Reply

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

Answer by Bunny83 · May 03, 2017 at 08:52 AM

The only thing that could really cause a Null ref exception here is your "colList". Are you sure you created an instance of your List?

So does your declaration looks like this:

 List<Collider2D> colList;

or like this:

 List<Collider2D> colList = new List<Collider2D>();

The latter would be correct. In the first case you only declared a variable that can hold an instance of a generic list but initially contains null;

Comment
Add comment · Show 3 · 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 Darn_ · May 03, 2017 at 11:29 AM 0
Share

Obviously doing the latter private List colList; void Start () { colList = new List(); //more stuff here...}

avatar image Bunny83 Darn_ · May 03, 2017 at 12:19 PM 0
Share

You shouldn't use Start for things like that. If you insist on using a callback from Unity, use Awake. Look, Unity never would call "OnTriggerEnter2D" or "OnTriggerExit2D" without passing a valid "Collider2D" as parameter. So we can be sure that other is not null (unless you call those methods manually which you should never do). Every valid Collider2D component can access it's gameObject it's attached to. "GetComponentInParent" could return null if no component was found. However since you don't use the returned value anywhere, it can't cause a null-ref-exception. The only thing left in the code you have posted is your "colList" variable.

Even if the object is somehow destroyed at the moment it enters, the error would be different. If the code you have posted is your actual code, it can only be your "colList" variable that is null.

avatar image Darn_ Bunny83 · May 03, 2017 at 02:38 PM 0
Share

Turns out using Start() was the problem. Thanks!

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

7 People are following this question.

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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

NullReferenceException script problem? 0 Answers

[Solved] Actually totally explainable NRE with 2D array 0 Answers

Null Reference Exception: Object Reference, with base class? 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