Follow

NullReferenceException

Symptoms

  • I am getting the error below appearing in my console:
NullReferenceException: Object reference not set to an instance of an 
object

Cause

This error is caused when an object is trying to be used by a script but does not refer to an instance of an object.

Resolution

To fix this example we can acquire a reference to an instance of the script using GameObject.Find to find the object it is attached to. We then use GetComponent to find the script component we want a reference to.

You can also double click the error to take you to the line of script where the error is occurring.

From there you can trace each object back to where they are assigned (if they have been assigned).

More Information

Was this article helpful?
6 out of 7 found this helpful
Have more questions? Submit a request

4 Comments

  • 0
    Avatar
    Marcos A Soto Cora

    Great.... Thanks!

  • 0
    Avatar
    Cameron Smith

    Thank you!

  • 0
    Avatar
    Jeremy

    In case anyone attempts this and it STILL doesn't work, let me recommend what worked for me.

    I was attempting to get an initiative value out of a Timeline script on my Units in a turn-based tactics game.

    But the Timeline is not directly ON the Unit, but on the initiative button that will be moving along the timeline UI element. It's down in Unit > Canvas > InitButton.

    For a situation like this, where you're setting up complex objects like a file structure, you can dig in to the specific script using GetComponentInChildren. The regular GetComponent won't find your script, because it's not directly on that containing "folder" object.

    Edited by Jeremy
  • 0
    Avatar
    Loladdwa12 Kais12

    Thanks NO HATE

Please sign in to leave a comment.