NullReferenceException: Object reference not set to an instance of an object
I get this error in unity: NullReferenceException: Object reference not set to an instance of an object PlayerSetup.OnDisable () (at Assets/Scripts/PlayerSetup.cs:96)
here is a screenshot of my playersetup script where the error comes from http://imgur.com/a/cA8fc
I don’t know what’s wrong pls help me
Answer by MeronSoda · Mar 15, 2017 at 03:09 PM
It seems your singleton GameManager has no instance.
1) Did you forget to attach your Component (GameManager) to any object in hierarchy?
2) In you GameManager script, did you assign a value for instance in the Awake function (not in Start function)?
void Awake () { instance = this; }
3) Is the object in hierarchy which holds the GameManager component active?
4) Are there any other scripts in scene that before running require GameManager to be already set up? If yes, consider defining an "Execution Order" in Edit > Project Settings > Script Execution Order.
$$anonymous$$eronSoda i tried to do my best about what you said but it still doesn't work. Here is screenshots of my Game$$anonymous$$anager. http://imgur.com/a/dg$$anonymous$$a7
Would you like to Collaborate and help me?
Answer by RecyclingBen · Mar 15, 2017 at 11:32 AM
It seems like you are trying to reference a gameobject (or some other kind of object) that is empty. If you see something in your inspector panel that says something like "empty (gameobject)" then drag over the gameobject/script/sprite/whatever you are trying to reference into it and it should work perfectly.
Answer by kpatulrpatel · Jul 30, 2020 at 09:09 AM
==> Create GameObject attach GameManager Script ==> In Awake() Method
private static GameManager instance;
public static GameManager Instance
{
get
{
if (!instance)
{
instance = FindObjectOfType(typeof(GameManager )) as GameManager ;
if (!instance)
{
Debug.LogError("There needs to be one active GameManager script on
a GameObject in your scene.");
}
}
return instance;
}
}
Awake()
{
instance = this;
DontDestroyOnLoad(this);
}
==> Add GameManager in Script Execution Order.