Sub Button shows on click

Hi guys, i am new to scripting how do i do that when a button is being clicked two sub buttons shows. I know that much :smile:

function OnGUI ()
{
GUI.Box(Rect(450,250,500,200), " Buttons");

if (GUI.Button (Rect (620,300,150,20), ā€œButton oneā€)) {
what should be here to display other buttons?
}

}

GUI.Button creates a button for you - but you already know that…
You want two new buttons? Call GUI.Button function twice with appropriate arguments.

You mean i need to define another function and if (GUI.Button (Rect (620,300,150,20), ā€œButton oneā€)) { the fucniton would be true?

You use only one OnGUI function to handle all gui inside it.
if (GUI.Button (Rect (620,300,150,20), ā€œButton oneā€)) { then create two new buttons.

if (GUI.Button (Rect (620,300,150,20), "Button one")) {
   if (GUI.Button (Rect (620,300,150,20), "SubButton one")) {
      //Do some stuff here
   }
   if (GUI.Button (Rect (620,300,150,20), "SubButton two")) {
      //Do some other stuff here
   }
}

so the code you wrote if we press button one two sub buttons gonna show? and yeah we will put something in sub buttons like Application.LoadLevel (2); or Application.Quit but i am confused about if we click on button one how it would show sub buttons

I did this way but it is not working.

function OnGUI()
{
    if (GUI.Button (Rect (620,300,150,20), "Button one")) {
       if (GUI.Button (Rect (620,300,150,20), "SubButton one")) {
          Application.LoadLevel (2);
       }
       if (GUI.Button (Rect (620,300,150,20), "SubButton two")) {
         Application.LoadLevel (3);
       }
    }
    }

No it won’t. My mistake. Checked out documentation and turns out the way it works is every frame OnGUI is called and GUI.Button will draw (or redraw rather) the button every frame so in code above our sub buttons would exist only for one frame after button one was clicked.
It actually should be:

void OnGUI()
{
   static bool butOneClicked = false;
   if(GUI.Button(new Rect(0,0,100,100), "Button one"))
   {
      butOneClicked = true;
   }
   if(butOneClicked)
   {
      if(GUI.Button(new Rect (100,100,200,200), "SubButton one"))
      {
         //Do some stuff here
      }
      if(GUI.Button(new Rect(0,0,100,100), "SubButton two"))
      {
         //Do some other stuff here
      }
   }
}

Except C# doesn’t have method scope static variables. Is there an alternative or does it have to be class scope?

Thank you man :slight_smile:

Just to make sure you got it right I will assume there is not alternative to method static variables and suggest this solution:

public class MyClass : MonoBehaviour
{
    private bool buttonOneCheck = false;
    public void OnGUI()
    {
       if(GUI.Button(new Rect(0,0,100,100), "Button one"))
       {
          buttonOneCheck = !buttonOneCheck;
       }
       if(buttonOneCheck)
       {
          if(GUI.Button(new Rect (100,100,200,200), "SubButton one"))
          {
             //Do some stuff here
          }
          if(GUI.Button(new Rect(0,0,100,100), "SubButton two"))
          {
             //Do some other stuff here
          }
       }
    }
}

Done :slight_smile: and working :slight_smile: