NullReferenceException a gameobject to another class

Hi I can’t understand why my code is throwing a nullreferenceexception.

But basically I want to make it so in a spaceship game when I shoot an enemy, that it makes a particle spawn at their transform.

The code works when I find a specific gameobject and pass that in method, but I don’t understand why it throws the exception when I pass the gameobject the enemy script is attached to:

In the enemy.cs

 private void OnParticleCollision(GameObject other)
    {
        Debug.Log("This is what other is: " + other);
        Debug.Log("This is what gameobject is: " + gameObject);

        particlesHandler = FindObjectOfType<ParticlesHandler>();
        particlesHandler.SpawnExplosionParticles(this.gameObject);
        Destroy(gameObject, 2f);      
    }
public class ParticlesHandler : MonoBehaviour
{
    [SerializeField] GameObject explosionParticles;

     public void SpawnExplosionParticles(GameObject objectToSpawnAt)
    {
        GameObject explosionvfx = Instantiate(explosionParticles,
            objectToSpawnAt.gameObject.transform.parent.position, Quaternion.identity);
        explosionvfx.transform.parent = objectToSpawnAt.transform;
    }
}

I did a test and passed :

        FindObjectOfType<PlayerController>().gameObject;

Which worked making the particle spawn the players ship.

This is the nullreference I’m getting:
NullReferenceException: Object reference not set to an instance of an object
ParticlesHandler.SpawnExplosionParticles (UnityEngine.GameObject objectToSpawnAt) (at Assets/Scripts/ParticlesHandler.cs:11)
Enemy.OnParticleCollision (UnityEngine.GameObject other) (at Assets/Scripts/Enemy.cs:21)

I can’t figure out why this is happening because gameobject in the enemy script does identify/find the correct gameobject that I have fired a laser at.

That’s all well and good but did you even establish that is what the problem is?

The answer is always the same… ALWAYS. It is the single most common error ever.

Don’t waste your life spinning around and round on this error. Instead, learn how to fix it fast… it’s EASY!!

Some notes on how to fix a NullReferenceException error in Unity3D

  • also known as: Unassigned Reference Exception
  • also known as: Missing Reference Exception
  • also known as: Object reference not set to an instance of an object

http://plbm.com/?p=221

The basic steps outlined above are:

  • Identify what is null
  • Identify why it is null
  • Fix that.

Expect to see this error a LOT. It’s easily the most common thing to do when working. Learn how to fix it rapidly. It’s easy. See the above link for more tips.

You need to figure out HOW that variable is supposed to get its initial value. There are many ways in Unity. In order of likelihood, it might be ONE of the following:

  • drag it in using the inspector
  • code inside this script initializes it
  • some OTHER external code initializes it
  • ? something else?

This is the kind of mindset and thinking process you need to bring to this problem:

https://discussions.unity.com/t/814091/4

Step by step, break it down, find the problem.

Here is a clean analogy of the actual underlying problem of a null reference exception:

https://discussions.unity.com/t/840647/7

1 Like

Thank you for such a detailed response with practical wisdom for dealing with this issue for now and the future.

I was just in the middle of typing to you that I had tried a load of different steps including attaching visual studios debugger and looking at the values of the variables.

And then I thought I will try something else and then I noticed in my code:

GameObject explosionvfx = Instantiate(explosionParticles,
            objectToSpawnAt.gameObject.transform.parent.position, Quaternion.identity);

In the objectToSpawnAt.gameObject.transform.parent.position, my players ship has a parent object, unlike the enemy ships don’t.

So even though the variables being passed had values. I’ve just realized I was trying to pass the transform to a parent of the enemy that doesn’t even exist.

I just changed the code to:

GameObject explosionvfx = Instantiate(explosionParticles,
            objectToSpawnAt.gameObject.transform.position, Quaternion.identity);

No wonder I was confused but thanks for helping me spot it.

1 Like