- Home /
Unity GUI Style with if Statements
I need to make a menu. With the script I have, if a certain level is unlocked, the GUI button turns red, indicating to the player that they can now play that level. I know to make a button work, the code would be:
if (GUI.Button (Rect (20,40,80,20), "Level 1")) {
Application.LoadLevel (1);
But my GUI has this code:
if(renderMenu && houseNineUnlocked && !explore){
GUI.backgroundColor = Color.red;
GUI.contentColor = Color.white;
GUI.Button(Rect(700,720,500,50), ("House Nine"));
As you can see, there are multiple components to this, and a set of standards has to be met for the GUI to be rendered, and it has specifics too, like background color, and content color. I can't put that into an "if" statement, so how would I do it?
(The only other way I can think of is using textures, but since the textures aren't being worked on at the moment, that's not an option.)
Answer by syclamoth · Oct 03, 2011 at 03:42 AM
You can nest if statements inside of other if statements! (I know, it's mind-blowing)
if(renderMenu && houseNineUnlocked && !explore) {
GUI.backgroundColor = Color.red;
GUI.contentColor = Color.white;
if(GUI.Button(Rect(700, 720, 500, 50), "House Nine"))
{
Application.LoadLevel(1);
}
}
Of course, this will not even render the button if the conditions are not met. Did you want the button to still render, but be greyed out?
The if statement I write in my questions is also accompanied by an else if statement too. I actually knew that you can put an if statement inside another if statement, but I didn't think it applied to GUI. Well apparently it doesn't because it's not working. Here an example of what I did this time:
if(render$$anonymous$$enu && houseOneUnlocked && !explore) {
GUI.backgroundColor = Color.red;
GUI.contentColor = Color.white;
if(GUI.Button(Rect(700, 320, 500, 50), "House One"))
{
Application.LoadLevel("House One");
}
}
It looks correct to me. The level is in the build settings, I'm getting no error, and all the booleans meet their requirement. I even tested the button, but the button didn't work. It's rendering okay. I'm confused.
Put some debug.Log just before the Application.LoadLevel bit. See if it is executing those lines!
I did, and nothing happened. Like I said, I tested the button, but the button didn't work. There's something wrong with the script.
So, your Debug.Log didn't print? I'd have a look at exactly what your button is doing. You say the button definitely renders? I'm telling you, it's nothing to do with nested if statements- there is absolutely no reason why that shouldn't work!
Do you want the whole script so you can test it out yourself?
Your answer
![](https://koobas.hobune.stream/wayback/20220612233554im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How do I make a "move pad" on the screen? (Mobile) 1 Answer
Making GuiButton trigger on ps3 controller input 0 Answers
Cases if a GUI.Button is clicked two or more times?? 2 Answers
GUI RepeatButton can only be clicked once 2 Answers
How can I make GUI button send an input message when I press it? 0 Answers