• 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 destructivArts · Jun 05, 2014 at 09:59 AM · gameobjectnullreferenceexceptiononcollisionenter

OnCollisionEnter GameObject throws NullReferenceException

I have two game objects with box colliders. One has a rigidbody. This is the player. The other object is a room. I am trying to test to see if the player is in the room. This behavior will be cloned across multiple rooms so its kind of a player tracking system.

When the player enters the rooms box collider, OnCollisionEnter fires. I pass the gameObject of the player to the roomController where I try and add the gameObject parameter (the player) to a List. The problem is that it throws a null reference exception. One of the things that I noticed when I stepped through my code is that the gameObject > base > gameObject > base trail seems to go on forever.

Another note: InteractableController (from which PlayerController inherits) is a class that inherits from MonoBehavior and only implements one virtual method called Interact() that is empty. So I don't think thats the problem.

 public class PlayerController : InteractableController {
     void OnCollisionEnter(Collision collisionInfo){
         if (CollisionType.getCollisionType(collisionInfo.collider) == CollisionType._CollisionType.room_trigger){
             collisionInfo.collider.gameObject.GetComponent<RoomController>().RoomEntered(gameObject);
         }
         LogCollision(collisionInfo);
     }
 
     void OnCollisionExit(Collision collisionInfo){
         if (CollisionType.getCollisionType(collisionInfo.collider) == CollisionType._CollisionType.room_trigger){
             collisionInfo.gameObject.GetComponent<RoomController>().RoomExited(transform.gameObject);
         }
         RemoveCollision (collisionInfo);
     }
 }

 public class RoomController : MonoBehaviour {
     List<GameObject> charactersInRoom;
 
     void Active () {
         charactersInRoom = new List<GameObject>();
     }
 
     public void RoomEntered(GameObject source){
         charactersInRoom.Add (source);
     }
 
     public void RoomExited(GameObject source){
         charactersInRoom.Remove (source);
     }
 }

And my stack trace

 NullReferenceException: Object reference not set to an instance of an object
 RoomController.RoomEntered (UnityEngine.GameObject source) (at Assets/Scripts/Gameplay/RoomController.cs:31)
 RoomController.OnCollisionEnter (UnityEngine.Collision info) (at Assets/Scripts/Gameplay/RoomController.cs:40)
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

0 Replies

· Add your reply
  • Sort: 

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

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

21 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

Related Questions

NullReferenceException 2 Answers

Destroy object last on application close. 1 Answer

Check if collider is colliding with anything non specific? 1 Answer

Why does my bullet prefab get destroyed? 1 Answer

Allocating Game Object name dynamically using Find 0 Answers


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