Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Make GUI.Button create another GUI.Button

Discussion in 'Immediate Mode GUI (IMGUI)' started by Pyronide, Oct 30, 2014.

  1. Pyronide

    Pyronide

    Joined:
    Jun 7, 2014
    Posts:
    56
    I am trying to make a simple, virtual pet game, but I am having issues with GUI.Buttons.

    I would like to have:

    Code (csharp):
    1.  GUI.Button(blah blah blah) //Menu button
    2.            GUI.Button(more blahs) //Feed button.
    Now, I can get both buttons to work by themselves, but when I put the second inside the first it doesn't work. I also tried a version with Instantiate variables to instantiate gui.button but it also didn't work.

    Any help with this would be greatly appreciated.
     
  2. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,670
    It's immediate mode. The first GUI.Button() will only be true for the instant that the button is clicked. Keep track of this click to show the second button.
    Code (csharp):
    1. bool showSecondButton = false;
    2.  
    3. void OnGUI() {
    4.     if (GUI.Button(blah blah blah)) {
    5.         showSecondButton = true; // (or = !showSecondButton to toggle)
    6.     }
    7.     if (showSecondButton) {
    8.         GUI.Button(more blahs);
    9.     }
    10. }
     
  3. Pyronide

    Pyronide

    Joined:
    Jun 7, 2014
    Posts:
    56
    This helps a lot, thank you very much.
     
  4. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,670
    Happy to help!