- Home /
Script is losing references once gameobjects get disabled. How can I fix this?
Hi :)
I have a couple GameObjects in my scene with tags attached. In my start function, I will store references to them in variables (this works) and disable these GameObjects afterwards, since the player doesn't need to know they are there yet (this works, too).
But now, I want to re-enable these GameObjects when the player presses a UI Button, which leads to "Object reference not set to instance of an object", which makes me tear my hair out.
Code-example looks like this; the Cape is tagged with "FF_Capa" and will be found and lost in the start-method:
public GameObject superHeroCape;
public void Start()
{
superHeroCape = GameObject.FindWithTag("FF_Capa");
superHeroCape.SetActive(false);
}
public void capa ()
{
superHeroCape.SetActive(true);
}
How can I make my script understand that it shouldn't lose its references?
I kind of feared this would be the case... :/ I commented the line about "setActive false" out and it was working as intended in void capa (), so it has to do with disabling it, as far as I understand. Plus, this is not the only instance of this happening; at least three other references get lost like this right after finding the corresponding GameObjects... But thank you for your input. Guess I 'll have to hope for a miracle then ;)
Are you sure the null reference is down to superHeroCape and not the object you are trying to access from the UI button
Your answer
