- Home /
Trying to find() an image in canvas & getting NullReferenceException
I've created a canvas with an image inside, and I want to programatically disable the image. Here's my script:
GameObject.Find("AvatarDefault").SetActive(false);
But this returns an error - NullReferenceException: Object reference not set to an instance of an object. Not sure why, because the object is definitely there, and named correctly.
I have been facing similar issues. If I tried to debug whether the parent canvas was found, that was also null!! Worked around by attaching the child gameobjects through the inspector.
If it's a child object maybe try referencing the pathway to it. Something like GameObject.Find("Canvas/AvatarDefault") to reference the image as a child of the canvas If that doesn't work, then I'd say digzou's idea of attaching it through the inspector is the best bet.
I've solved this problem a different way now doing something different, but yeah, crazy that you can't just disable an image the same way as a normal gameobject!
Answer by SylvesterAHansen · Jul 15, 2015 at 09:49 PM
Hey there. If you have an image in a canvas, then you won't be bale to setActive. Images as UI elements only responde to enable = true/false. I don't know why it is like that, but that is how it works
Your answer
Follow this Question
Related Questions
Canvas Scaler settings 0 Answers
Force UIImage to stay on viewport? 0 Answers
Changing a sprite in image canvas VIA C# script 2 Answers
RenderTexture does not render canvas image. 0 Answers
Unity 2019 Everything in Canvas goes Invinsible, wont come back. 0 Answers