hello guys, I want to create a button which will create another button on a Gui system, but it seems to does not work. Is there any other way to do that?
code :
if(GUI.Button(Rect(335, 80, 50, 50), "Button1"))
{
if(GUI.Button(Rect(335, 140, 50, 50), "Button2"))
{
print("you clicked on the 2nd button");
}
}
You probably need to store the state of the button in some variable and use this to display the second button.
var button1Clicked = false;
function OnGUI()
{
if (GUI.Button(new Rect(335, 80, 50, 50), "Button1"))
{
button1Clicked = true;
}
if (button1Clicked)
{
if (GUI.Button(new Rect(335, 140, 50, 50), "Button2"))
{
print ("you clicked on the 2nd button");
}
}
}
I think (I can't remember the exact details) your code doesn't work because OnGUI gets called multiple times per frame (layout pass, input pass ...). During the layout pass the state of button presses will always be false and therefore your first if statement is always false.