- Home /
Can't create a button when clicked?
I have a toolbar. When I click the first button, I was expecting the GUI to create a new button but it didn't. why?
CODE:
private Rect fsad = new Rect (120, 120, 500,500);
void OnGUI () {
if (skin != null) {
GUI.skin = skin;
}
GUI.Box(new Rect(0,0,Screen.width,Screen.height),"Pause menu");
toolbarInt = GUI.Toolbar (new Rect(0,50, Screen.width,50),toolbarInt, toolbarStrings);
if (GUI.changed)
{
Debug.Log("The toolbar was clicked");
if (0 == toolbarInt)
{
GUI.Button (fsad, "Button");
}
if(1 == toolbarInt){
Debug.Log("Second button was clicked");
}
if(2 == toolbarInt){
}
if(3 == toolbarInt){
}
}
}
Answer by Joyrider · Aug 20, 2013 at 09:10 AM
The GUI fonction it something that is parsed a number of times per frame. All that is displayed on screen is what gets parsed last before rendering on screen.
In your case, GUI.changed will only return true once, at the moment your GUI has changed. And revert back to false afterwards. So your new button will only be parsed and displayed on screen (if at all) for one frame.
under the GUI.changed, you should change a variable.
and use that variable to show the new button or not. But not have the button under GUI.changed.
if (GUI.changed)
{
Debug.Log("The toolbar was clicked");
if (0 == toolbarInt)
{
showNewButton = true;
}
}
if(showNewButton)
{
if(GUI.Button(...))
}
Your answer
Follow this Question
Related Questions
Cases if a GUI.Button is clicked two or more times?? 2 Answers
Adding a texture to a GUI button 1 Answer
Distribute terrain in zones 3 Answers
is using OnPostRender to draw textures Pro only? 3 Answers
GUI Buttons not Being Displayed 1 Answer