Search Unity

NullReferenceException - where have I gone wrong?

Discussion in 'Scripting' started by hallidev, Feb 9, 2017.

  1. hallidev

    hallidev

    Joined:
    Jan 21, 2017
    Posts:
    14
    I'm wasting entirely too much time on this. It really has me confused.

    The following line (below) is throwing a NullReferenceException:
    BlowsUpOnParticleCollisions collisionReceiver = particle.collisionCollider != null ? particle.collisionCollider.gameObject.GetComponent<BlowsUpOnParticleCollisions>() : null;

    I wrapped this in a try/catch, and "particle.CollisionCollider" appears to be null. I can't understand how this is possible, even in a freakish threading scenario. I've guarded against nulls before attempting the call and then again inline.

    I should note that when GetComponent doesn't return null this works. Not all of the objects in my scene "blow up on particle collision". Even though this script throws copious exceptions, the effect I'm going after still works...I just can't have my script throwing exceptions with wild abandon.

    onParticleCollision is an event.

    particle.collisionCollider is of type Collider - so it's a reference type (class).

    Code (CSharp):
    1.     private void onParticleCollision(PlaygroundEventParticle particle)
    2.     {
    3.         if(particle != null && particle.collisionCollider != null && particle.collisionCollider.gameObject != null)
    4.         {
    5.             BlowsUpOnParticleCollisions collisionReceiver = particle.collisionCollider != null ? particle.collisionCollider.gameObject.GetComponent<BlowsUpOnParticleCollisions>() : null;
    6.  
    7.             if (collisionReceiver != null)
    8.             {
    9.                 collisionReceiver.CurrentCollisionCount++;
    10.             }
    11.         }
    12.     }
    Where have I gone wrong?

    I'll cry if it's something super obvious :)
     
  2. dterbeest

    dterbeest

    Joined:
    Mar 23, 2012
    Posts:
    389
    try some Debug.Log statements throughout this piece of code.
     
  3. Pengocat

    Pengocat

    Joined:
    Dec 7, 2016
    Posts:
    140
    Why not use the built in method "OnParticleCollision(GameObject other)" instead of making your own event?
     
  4. hallidev

    hallidev

    Joined:
    Jan 21, 2017
    Posts:
    14
    First, thanks for the responses.

    Putting in Debug.Log statements is also blowing my mind. The NullReferenceExceptions just don't happen when it's in there.

    The following always shows "Colider not null". Also, my console doesn't show any exceptions. Everything slows to an absolute crawl, and I guess that's a hint that something odd is going on...

    Code (CSharp):
    1. Debug.Log(particle.collisionCollider != null ? "Collider not null" : "Collider null");
    Regarding OnParticleCollision - I'm using Particle Playground 3, so events are hooked up like:

    Code (CSharp):
    1. _particleSystem.events.First(e => e.eventType == EVENTTYPEC.Collision).particleEvent += onParticleCollision;
    Under the cover Particle Playground wraps a Shuriken particle system though, so I'll give that a try.

    Still - I just can't for the life of me determine what is going on with this seemingly simple piece of code.
     
  5. Deleted User

    Deleted User

    Guest

  6. hallidev

    hallidev

    Joined:
    Jan 21, 2017
    Posts:
    14
    NullReferenceException shows as "Object reference not set to an instance of an object."

    See here:
    https://msdn.microsoft.com/en-us/library/system.nullreferenceexception(v=vs.110).aspx

    Description:
    A NullReferenceException exception is thrown when you try to access a member on a type whose value is null. A NullReferenceException exception typically reflects developer error and is thrown in the following scenarios...

    They have to throw it in my face with "typically reflects developer error" :)
     
  7. hallidev

    hallidev

    Joined:
    Jan 21, 2017
    Posts:
    14
    Can any Unity pros help out here?