Why does GameObject.Find() work in ones and not in others?
Hi! That's the point: I have a list of gameObjects called "Diana". When I firstly use :
GameObject.Find("Diana").SetActive(false);
It works perfectly. But then, when I call:
GameObject.Find("Diana").SetActive(true);
it shows me a NullReferenceException:
NullReferenceException: Object reference not set to an instance of an object
Here's an image of my code and the Hierarchy.
Can anybody help me?
Thank in advance!
Answer by gjf · Nov 30, 2015 at 03:27 PM
although you've combined it with the hierarchy screenshot, when posting code, please paste it and format appropriately. if somebody helping you needs to include it, they're unlikely to type it in.
that said, i believe your problem is that when you disable a game object, Find
will no longer, err, find it - because it's no longer active. in order to achieve this functionality, store a reference in Awake()
or Start()
and use it when necessary.
it's a good idea to check for null
before accessing it too...