NullReferenceException: Object reference not set to an instance of an object.

Hello, i have this error in my script : NullReferenceException: Object reference not set to an instance of an object.

It doesn't really bother me since I can put play and play normally. But I would like to know what error is causing this problem. Thanks again for your help!

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class turbo : MonoBehaviour
{
    public ParticleSystem SetParticles;


    // Start is called before the first frame update
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetMouseButtonDown(1))
        {
            SetParticles.Play();

        }
    }
}

[quote=“elyes51”, post:1, topic: 840647]
NullReferenceException: Object reference not set to an instance of an object.
[/quote]

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.

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.

1 Like

You trying to use script ParticleSystem but you must find it first and this script must be placed on any go in scene.

1 Like

In my scene I have:

-only 1 SetParticles
-only 1 particlesystem

And when I press play everything is ok. But the error persists.

[quote=“elyes51”, post:4, topic: 840647]
But the error persists.
[/quote]

And you persist in not following step 1 above to even discover what is throwing the error!

I have run the script about 30 times removing and modifying a single parameter each time. I think the problem is with public ParticleSystem SetParticles; because it cannot find ParticleSystem in the script.

Is your particle system assigned to the field in the Inspector on your script?

A Null Reference Exception (NRE) is caused when you're trying to access a method or property on a null object -- in other words, trying to get something from nothing.

Pretend I give you a set of instructions:

  • I will hand you a package of Oreos.
  • Open the package
  • Take out a cookie.
  • Eat the cookie.
  • Close the package.

I hand you the package, you open it, and the container is empty. Your "code" would fail at line 4, because you'd be trying to eat a cookie that doesn't exist. That's an NRE.

@Kurt-Dekker gave you some great tips on how to find that problem. @MartinMa just straight up gave you the answer. What part are you having difficulty understanding?

2 Likes

This is a GREAT analogy @Schneider21 ! But ... technically wouldn't it fail at step 3 when you first try to dereference the cookies? :)

I'm gonna keep a link to your post. Good stuff!

I leave you with a quote from Sesame Street here:

"C is for cookie and that's good enough for me!" om nom nom nom...

1 Like

First of all thank you for your time and your understanding (knowing that I discovered unity, 3 weeks ago)

according to this comment :

You trying to use script ParticleSystem but you must find it first and this script must be placed on any go in scene.

My script is placed on the player who will play the particle animation.
What I would not have understood is to find it first!


You're right! The first time I used this analogy, I was using a single object. The idea being that fetching that object didn't throw an error, but returned null. I've updated the cookie example to reflect that thinking.