- Home /
GUI.Button not appearing when called, no errors
Hello everyone,
In this snippet of code I would like to have a "Left" button appear after the "Equip" button is pressed. However, the "Left" button never appears. I know the "Equip" button IS being pressed because I placed Debug.Logs in it and they were called. Also I made sure that the x,y,z transform of the button is within screen borders. No errors are appearing, so I am clueless as to why this button is not showing up on my screen.
Yes there is an if statement before this else if. and the selected variable remains the correct value the entire time.
Let me know if you need more information. Also this is in Javascript.
EDIT: Sorry, I have also attempted to use a boolean to call the "Left" button after the "Equip" button is pressed. That did not work.
//Arm
else if(allitems[selected].itemType == "Arm")
{
if(GUI.Button(Rect(Screen.width / 2 - 225,Screen.height / 2 - 250 + (selected * 35), 150, 30), GUIContent("EQUIP")))
{
if (GUI.Button(Rect(10,70,50,30),"Left"))
{
Debug.Log("Button: 'LEFT' Pressed");
}
}
}
Answer by Bunny83 · Jun 06, 2017 at 11:15 PM
You're using the immediate mode GUI (IMGUI). The immediate mode GUI is basically "stateless". OnGUI is called by unity to process different events. During the Repaint event all control methods that are invoked during that call will actually draw their control. However the body of your if statement is only executed once during a MouseUp event. So placing any GUI element inside the if body of another button is completely pointless.
If you want to show the left button after you clicked on the equip button, you need to define a variable to store that state:
// UniteScriot
var showLeftBut = false;
function OnGUI()
{
// [ ... ]
if(GUI.Button(Rect(Screen.width / 2 - 225,Screen.height / 2 - 250 + (selected * 35), 150, 30), GUIContent("EQUIP")))
{
showLeftBut = true;
}
if (showLeftBut)
{
if (GUI.Button(Rect(10,70,50,30),"Left"))
{
Debug.Log("Button: 'LEFT' Pressed");
}
}
// [ ... ]
}
If you need more information on the IMGUI system, see my GUI crash course.
Note that the IMGUI is not very optimised and creates one or several drawcalls per control, The new uGUI system is more optimised. It uses a component based approach so you can design your GUI visually in the editor.
Your answer
Follow this Question
Related Questions
GUIText Problem With MENU 1 Answer
Gui Button Solid 2 Answers
GUI button working with GUI Textfield 0 Answers
Use a key to open a GUI? 2 Answers
On Clicked, On Released GUI Button ? 2 Answers