Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

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

Discussion in 'Scripting' started by elyes51, May 12, 2021.

  1. elyes51

    elyes51

    Joined:
    Feb 24, 2021
    Posts:
    62
    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!


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class turbo : MonoBehaviour
    6. {
    7.     public ParticleSystem SetParticles;
    8.  
    9.  
    10.     // Start is called before the first frame update
    11.     void Start()
    12.     {
    13.  
    14.     }
    15.  
    16.     // Update is called once per frame
    17.     void Update()
    18.     {
    19.         if (Input.GetMouseButtonDown(1))
    20.         {
    21.             SetParticles.Play();
    22.  
    23.         }
    24.     }
    25. }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,519
    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://forum.unity.com/threads/why-do-my-music-ignore-the-sliders.993849/#post-6453695

    Step by step, break it down, find the problem.
     
    elyes51 likes this.
  3. MartinMa_

    MartinMa_

    Joined:
    Jan 3, 2021
    Posts:
    455
    You trying to use script ParticleSystem but you must find it first and this script must be placed on any go in scene.
     
    elyes51 likes this.
  4. elyes51

    elyes51

    Joined:
    Feb 24, 2021
    Posts:
    62
    In my scene I have:

    -only 1 SetParticles
    -only 1 particlesystem

    And when I press play everything is ok. But the error persists.
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,519
    And you persist in not following step 1 above to even discover what is throwing the error!
     
  6. elyes51

    elyes51

    Joined:
    Feb 24, 2021
    Posts:
    62
    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.
     
  7. Schneider21

    Schneider21

    Joined:
    Feb 6, 2014
    Posts:
    3,512
    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:
    1. I will hand you a package of Oreos.
    2. Open the package
    3. Take out a cookie.
    4. Eat the cookie.
    5. 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. @metixgosu just straight up gave you the answer. What part are you having difficulty understanding?
     
    Last edited: May 13, 2021
    beinzheans and cocparas7 like this.
  8. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,519
    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...
     
    Schneider21 likes this.
  9. elyes51

    elyes51

    Joined:
    Feb 24, 2021
    Posts:
    62
    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!
     
  10. Schneider21

    Schneider21

    Joined:
    Feb 6, 2014
    Posts:
    3,512
    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.