- Home /
GUI.Label Not Disappearing
Here's my code:
var toggleBox = false;
var toggleLabel = false;
var customSkin : GUISkin;
function OnGUI(){
GUI.skin = customSkin;
// Make a background box
GUI.Box(new Rect(10,10,1260,100), "Planet Domination");
// Make the first button. If it is pressed, Application.Loadlevel (1) will be executed
if(GUI.Button(new Rect(20,40,100,30), "Singleplayer")) {
Application.LoadLevel(1);
Destroy(this);
}
if(GUI.Button(new Rect(150,40,100,30), "Multiplayer")) {
Application.LoadLevel(1);
Destroy(this);
}
if(GUI.Button(new Rect(280,40,100,30), "Options")) {
Application.LoadLevel(1);
Destroy(this);
}
if(GUI.Button(new Rect(410,40,100,30), "About")) {
toggleBox = true;
toggleLabel = true;
}
if (toggleBox == true)
if (toggleLabel == true)
GUI.Box(new Rect(10,150,500,200), "About the Game");
GUI.Label(new Rect(20,170,475,490), "Created in 2014 by Pixel-Made Studios");
}
When I put this code in, the GUI.Label won't disappear when the scene starts. Can anyone help me?
Are you sure the "Label" isn't getting rendered from another script in the other scenes. How about using Debug.Log
to find out whether that Label
is still getting called in the other scenes?
Answer by oatsbarley · Jan 25, 2014 at 09:34 AM
Your problem is the if-statements without braces:
if (toggleBox == true)
if (toggleLabel == true)
GUI.Box(new Rect(10,150,500,200), "About the Game");
GUI.Label(new Rect(20,170,475,490), "Created in 2014 by Pixel-Made Studios");
This actually means:
if (toggleBox == true) {
if (toggleLabel == true) {
GUI.Box(new Rect(10,150,500,200), "About the Game");
}
}
GUI.Label(new Rect(20,170,475,490), "Created in 2014 by Pixel-Made Studios");
This is because a braceless if-statement only runs to the next statement: the first one runs to the second one, and then the second one runs to the GUI.Box()
statement. The GUI.Label()
statement is completely outside of the if block. You have to use braces to do what you want here, so it'd be:
if (toggleBox == true) {
GUI.Box(new Rect(10,150,500,200), "About the Game");
if (toggleLabel == true) {
GUI.Label(new Rect(20,170,475,490), "Created in 2014 by Pixel-Made Studios");
}
}
I tend to always use braces with if-statements, and it saves me any confusion about where the if block ends. If you want to use braceless if-statements wherever you can, though, the if (toggleLabel)
doesn't need braces.
Your answer
Follow this Question
Related Questions
Drag Script Not Working 1 Answer
How to add a peaking system? 1 Answer
Attach object as child using code 1 Answer
enemy mechanics script and declining health 1 Answer
Detect face of cube clicked 4 Answers