I have some trees in my placed prefab, which I switch off with a button. The same button is also suppose to switch it back on (make active), but for the life of me I cant figure out how to get past the error: “Object reference not set to an instance of an object”
Gameobject myTrees;
public void ShowHideTrees()
{
myTrees = GameObject.FindGameObjectWithTag("Trees");
if (myTrees.active)
{
myTrees.SetActive(false);
}
else
{
myTrees.SetActive(true);
}
}
The trees are switched off fine with the button, but when I want to switch them back on, I get the dreaded “Object reference not set to an instance of an object” issue. I can’t make the GameObject public and assign it in the Inspector since the prefab is only placed after the app runs.
activeSelf or activeinHierarchy yields the same results so I’m afraid that’s no help at all.
Again, the object is clearly there, and in the scene at the time I run the code. What I dont understand is, why can I find it with it’s tag, then switch it off, but immediately after that, I can’t find it using same tag and switch it back on