- Home /
Unity ''forgets'' object reference after object has been disabled, and will not reenable it
I have an object (Canvas_Level) that is present and active in the scene, I have a gameManager object that sets it disabled (Game_Level.SetActive(false), and sets another object (Canvas_Map) enabled. (Canvas_Map.SetActive(true);
Each object is a different UI that needs to be displayed at different times. I have a button that is supposed to switch which UI is shown. It does this by setting the onbect either active or not.
void Start () {
canvasMap = GameObject.Find ("Canvas_Map");
canvasLevel = GameObject.Find ("Canvas_Level");
playerCharacter = GameObject.Find ("Player");
mapCamera = GameObject.Find ("MapCamera");
ActivateMap ();
}
void ActivateMap(){
canvasLevel.SetActive (false);
playerCharacter.SetActive (false);
mapCamera.SetActive (true);
canvasMap.SetActive (true);
MapActive = true;
}
void ActivateLevel(){
MapActive = false;
mapCamera.SetActive (false);
canvasMap.SetActive (false);
canvasLevel.SetActive (true);
playerCharacter.SetActive (true);
}
(there is also a method, linked to a button, that calls the ActivateLevel() method.
However, after the objects have been disabled by the script, unity "forgets" the references to them is throws an error when i try reenable them.
An object reference can be null because A) it hasn't been assigned B) it's a Unity object that has been Destroyed (or lost in scene loading) C) null has been assigned as its value
You have to find out which of these is the case.
Calling SetActive(false) on a gameObject will definitely not null any references to it, but if you disable a GameObject and then call your Start() method again, for example, Unity won't find the disabled objects and will assign a null to those variables.
Are you using the game$$anonymous$$anager to do ActivateLevel() or are you using some other class which has no references to call it?
So far it only looks like your game$$anonymous$$anager has references and then it turns them all off, depending on the order the scene loads your other objects won't be able to find them if the game$$anonymous$$anager has turned them off. But they can just find your game$$anonymous$$anager and use the game$$anonymous$$anager's references.
Follow this Question
Related Questions
Rigidbody help 0 Answers
Can't reactivate object 1 Answer
Error BCE0019 1 Answer
Invalid SceneHandle 0 Answers
Player using turret problem 1 Answer