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

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)

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.

1 Like

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:

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!

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.

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:

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

1 Like

Don’t feel dumb. Every game developer learned how to troubleshoot a null reference error for the first time at some point.

3 Likes