- Home /
Unity GUI Nested If statements causing GUI Buttons not to render
I have a Map Interface that has a series of buttons that when clicked will load a new scene, effectively a "Fast Travel" mechanic. Currently there is only 1 button available to click, whihc returns the player to their Home Village. Initially I just had it load the Home Village scene, and everything worked perfectly. Now I'm trying to add a pop-up so when the player tries to return home, it will first create a label asking "Are you sure you want to return home?" and create a "Yes" and "No" button. When I tried to add nested if statements to achieve this howevoer, the label and 2 buttns are not rendered for some reason. Is there a known problem with nested GUI-related If statements? My code of OnGUI() is below.
function OnGUI(){ // If the map has been opened, create all of the location buttons if(mapOpenp == true){ mapInterface.enabled = true;
// If the Home Village button is clicked...
if( GUI.Button((new Rect (500, 400, 50, 50)), homeVillageBtnTxtr, homeVillageStyle) ){
Debug.Log("Home Village Button Clicked");
//Application.LoadLevel("BuildingInterface");
// The above line, when uncommented, loads the BuildingInterface scene when the
// button is clicked, testing that the button itself works correctly.
// ... create a label asking if they want to return home,
GUI.Label((new Rect (100, 200, 75, 75)), "Are you sure you want to return home?");
// ...and create a Yes and No button. If Yes is clicked, load the Building Scene.
// If no is clicked, close the Map interface.
if( GUI.Button((new Rect (100, 250, 50, 50)), "Yes") ){
Debug.Log("Yes button has been created + clicked");
Application.LoadLevel("BuildingInterface");
}
if ( GUI.Button((new Rect (150, 250, 50, 50)), "No") ){
Debug.Log("No button has been created + clicked");
GUI.enabled = false;
}
}
}else{
// Otherwise the map has been closed, so disable the buttons
GUI.enabled = false;
}
}
Solution (thanks to Eric5h5) using a boolean variable rather than nested If statements:
function OnGUI(){ // If the map has been opened, create all of the location buttons if(mapOpenp == true){ mapInterface.enabled = true;
// If the Home Village button is clicked...
if( GUI.Button((new Rect (500, 400, 50, 50)), homeVillageBtnTxtr, homeVillageStyle) ){
// ... set the "has the Village button been clicked" boolean to true.
homeVillageBtnp = true;
}
// If the Village button has been clicked...
if(homeVillageBtnp){
// ...create a label asking if they want to return home...
GUI.Label((new Rect (100, 200, 75, 75)), "Are you sure you want to return home?");
// ...and check if the Yes button has been clicked...
if( GUI.Button((new Rect (100, 250, 50, 50)), "Yes") ){
// If the yes button is clicked, load the Building Interface
Application.LoadLevel("BuildingInterface");
}
// ...and check if the No button has been clicked.
if ( GUI.Button((new Rect (150, 250, 50, 50)), "No") ){
// If the no button is clicked, close the "pop-up"
homeVillageBtnp = false;
}
}
}else{
// Otherwise the map has been closed, so disable the buttons
GUI.enabled = false;
}
}
Answer by Eric5h5 · Aug 04, 2012 at 08:32 PM
GUI buttons only return true for the one frame in which they are clicked. So everything inside the "if (button)" statements will only render for part of one frame and therefore not be visible. Think of OnGUI as being like Update. Instead, you can use GUI.Button to toggle a state and query the state as to whether additional buttons should display or not.
Ah I see, so if I create a boolean variable and within the if(button) statement set it to true and then query that boolean for my Yes/No buttons it should work. Thank you for clearing that up, I didn't know that about GUI buttons.
Your answer
Follow this Question
Related Questions
If statement is not working properly 1 Answer
GUI.Button with double functionality 2 Answers
GUI.Style is messing up big time 0 Answers