- Home /
How to hide/show GUI Buttons through another GUI Button?
Hi all,
I'm a new Unity user and C# scripter and I want to be able to click a button to trigger the showing of several more buttons (this is for an options menu: sound/music toggle). I couldn't figure out how to show/hide Buttons OnGUI so I tried this: (didn't work)
public bool opt = false;
void OnGUI() {
if(GUI.Button(new Rect(Screen.width / 5, Screen.height * 13 / 30, Screen.width / 13, Screen.height / 30), "Opt") {
opt = !opt;
}
if(opt) {
if(GUI.Button(new Rect(Screen.width / 5, Screen.height * 13 / 30, Screen.width / 13, Screen.height / 30), "Music Off") {
//do
}
}
}
I have a feeling that GUI.Buttons can't be put inside if statements but can't figure out what to do... Any suggestions for this problem?
Answer by VolureDarkAngel · Jul 29, 2013 at 05:38 PM
Seems like you were on the right track. You just had some )'s missing in your if(GUI lines.
Here is a corrected version.
bool opt = false;
void OnGUI(){
if(GUI.Button(new Rect(Screen.width / 5, Screen.height * 13 / 30, Screen.width / 13, Screen.height / 30), "Opt")) {
opt = !opt;
}
if(opt) {
if(GUI.Button(new Rect(Screen.width / 3, Screen.height * 13 / 30, Screen.width / 13, Screen.height / 30), "Music Off")) {
//do
}
}
}
I also moved the Music button so it will not be directly on top of the Opt button.
@mntng - @VolureDarkAngel has the same answer, so I deleted $$anonymous$$e. Please click the checkmark next to the answer to close it out.
Answer by akkiDev · Apr 25, 2014 at 11:25 AM
Try This..
bool opt = false;
void OnGUI(){
if(GUI.Button(new Rect(Screen.width / 5, Screen.height * 13 / 30, Screen.width / 13, Screen.height / 30), "Opt")) {
opt = !opt;
}
if(opt && GUI.Button(new Rect(Screen.width / 3, Screen.height * 13 / 30, Screen.width / 13, Screen.height / 30), "Music Off")) {
//do
}
}
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Cases if a GUI.Button is clicked two or more times?? 2 Answers
Distribute terrain in zones 3 Answers
How do I access a GUITexture from C#? 0 Answers