Search Unity

collision.gameObject returning Null

Discussion in 'Physics' started by dhdylan, Aug 27, 2019.

  1. dhdylan

    dhdylan

    Joined:
    Jul 26, 2018
    Posts:
    3
    I have an OnCollisionEnter() and I'm trying to add the GameObject that is being collided with to a list of GameObjects. Once the collision happens I get the dreaded NullReferenceException: Object reference not set to an instance of an object.

    Here is my code:

    Code (CSharp):
    1. public class BulletCollision : MonoBehaviour
    2. {
    3.     [SerializeField]
    4.     private GameEvent OnCharacterHit;
    5.  
    6.     private void OnCollisionEnter(Collision collision)
    7.     {
    8.         GameObject colObject = collision.gameObject;
    9.         Debug.Log(colObject);
    10.  
    11.         EventListenerFilter filter = new EventListenerFilter();
    12.         filter.raisingObjects.Add(colObject); //this is where the exception is thrown
    13.  
    14.         OnCharacterHit.RaiseFilterByObjects(filter);
    15.     }
    16. }
     
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,459
    So you're saying that "Debug.Log(colObject)" outputs null?

    Doesn't the exception tell you more info such as what is actually giving you the null? Are you sure it's not the "raisingObjects" property itself?
     
  3. dhdylan

    dhdylan

    Joined:
    Jul 26, 2018
    Posts:
    3
    No the debug.log is not throwing an error. The line that the error is being thrown is on line 12. I’ll paste the actual error when I get home, but what unity is saying is that when I try to pass in the GameObject - that is being stored in “colObject” - it is returning null, thus throwing NullReferenceException.
    In fact what is even more surprising is that when i debug.log the object it displays properly. I actually changed that line to log the name of the object and that also popped up correctly.

    Edit: I forgot to mention that “filter.raisingObjects” is a list of gameObjects if you couldn’t tell.
     
  4. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,459
    Something doesn't "smell" right here. There's no way that the GameObject can exist at the Debug.Log statement then not exist adding it to the list nor would I expect a NullReferenceException from it.

    Does "Debug.Log(filter.raisingObjects.Count);" throw a NullReferenceException too? If so then it's as I suspected which is you're just not creating the list for the "raisingObjects" property.

    Pasting the full error would be useful to save guessing here.
     
  5. dhdylan

    dhdylan

    Joined:
    Jul 26, 2018
    Posts:
    3
    I’ve got a feeling that this may be the case. I’ll post the EventListenerFilter class on here momentarily, and try debug.logging the list as well.
     
  6. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,459
    As I said above though, the actual exception message should always state what object or property that is causing the issue so all the above back/forth shouldn't be required as it should cleary state it. As I said above, pasting the full error would be useful to save guessing here. No need to paste code.
     
    Vryken likes this.