Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

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

Discussion in 'Scripting' started by RojoPink, Apr 15, 2021.

  1. RojoPink

    RojoPink

    Joined:
    Apr 15, 2021
    Posts:
    3
    Aye-O I'm new to making games in Unity and doing my best to try out game creating by making my own Dino Run like game. I've ran into an error that I can't seem to solve. Any help is welcomed and thanks in advanced.

    NullReferenceException: Object reference not set to an instance of an object
    Dwaggo_OnTriggerEnter2D (UnityEngine.Collider2D collision) (at Assets/Scripts/Dwaggo.cs:51)

    NullReferenceException: Object reference not set to an instance of an object
    Dwaggo2.OnTriggerEnter2D (UnityEngine.Collider2D collision) (at Assets/Scripts/Dwaggo2.cs:29)
    One.png Two.png Three.png
     
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,529
    What kind of help do you need? The error is telling you the line where the NULL object is which is obviously the "gameManager" so that's not assigned. I mean you can even see that in the final image in the inspector.
     
    Joe-Censored likes this.
  3. RWKeska

    RWKeska

    Joined:
    Apr 15, 2021
    Posts:
    24
    The exception is thrown on line:
    You are trying to call a method GameOver of whatever object is being referenced by your variable gameManager. However at the time of that call your variable gameManager has value null as in none. The reason is that while you do declare the variable you never assign it any value so it keeps it default null value. Since your gameManager variable is public and exposed in the Inspector you can actually see in your Editor screenshot that the variable has no value assigned:
     
    Last edited: Apr 15, 2021
  4. RojoPink

    RojoPink

    Joined:
    Apr 15, 2021
    Posts:
    3
    Dang I feel dumb as heck I never noticed I was missing anything as it was working great before I start adding new stuff to the game but thanks got it fixed now!
     
  5. RojoPink

    RojoPink

    Joined:
    Apr 15, 2021
    Posts:
    3
    Thanks for the help! Once I started to add more new stuff to my game it stop working until now its fixed. I never noticed my gameManager got removed and learning all about Unity.
     
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,756
    Remember folks, a null reference does NOT require a forum post. The reason is this:

    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.
     
    MelvMay likes this.
  7. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Don't feel dumb. Every game developer learned how to troubleshoot a null reference error for the first time at some point.