- Home /
The question is answered, right answer was accepted
Turn Another UI image on and off with a Single UI Button
I have been trying to find a good simple way to simply turn on and off (Toggle) another UI image using a single UI button. Is there a simple way to do this?
The other image, do you show them by using a GUI function (something like DrawTexture) or do you made it a GameObject? The way to show/hide is somewhat different between the 2.
Just another UI image on a canvas, It's basically a lighted state for an indicator light to show when something is either active or not active but it's supposed to be turned on and off by a UI button also a child of the canvas. I hope that helps explain it better?
Answer by zaid87 · Jan 26, 2015 at 03:47 AM
I'm not familiar with using canvas but maybe something like this should do. Since from what I've seen, the image will still be a GameObject. I made the image to be public so you can assign it in the editor instead (drag&drop):
public GameObject image;
void OnGUI(){
if(GUI.Button(new Rect(0,0,100,100), “Toggle Image”)){
image.SetActive(!image.active);
}
}
Thanks, I didn't need the OnGUI part but the "image.SetActive(!image.active);" was the part of the puzzle I was missing, I used the public GameObject to set my image and the other part solved it. Thanks :)
Follow this Question
Related Questions
Buttons in unity: Text or Built in text in PNG? 2 Answers
[Partially Solved] OnClick of one button, show different set of buttons. 1 Answer
Do Something ONLY when all Toggles are On or Off 4 Answers
Button intractable area larger than sprite image 1 Answer
Instantiating Image makes whole Canvas disappear in Playmode 1 Answer