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

Simple GUI Problem.

Discussion in 'Scripting' started by Rastafari, Jan 8, 2013.

  1. Rastafari

    Rastafari

    Joined:
    Jul 18, 2011
    Posts:
    196
    Hello gentlemen
    Im trying to create a button wich succeeded.
    Now I if I click the button it should display another button.
    Wich is not working.
    I can use Debug.Log but when I try to create a new button it wont work.
    Here is my current code.

    Code (csharp):
    1. private var toggleTxt : boolean = false;
    2.  
    3. function OnGUI() {
    4.     if(GUI.Button(Rect(10,70,125,50),"Voice Chat On")){
    5.     GUI.Button(Rect(10,10,150,50),"I cant script");
    6.     }
    7.     toggleTxt = GUI.Toggle(Rect(10, 10, 50, 50), toggleTxt, "A Toggle text");
    8. }
    9.  
     
  2. ModStoryGames

    ModStoryGames

    Joined:
    Apr 27, 2012
    Posts:
    179
    You need to split those up and toggle a boolean to determine whether Button two is shown.

    Code (csharp):
    1.  
    2. private var toggleTxt : boolean = false;
    3. private var showNextButton : boolean = false;
    4.  
    5. function OnGUI()
    6. {
    7.    if(GUI.Button(Rect(10,70,125,50),"Voice Chat On"))
    8.    {
    9.       showNextButton = !showNextButton;
    10.    }
    11.  
    12.    if (showNextButton)
    13.    {
    14.       if (GUI.Button(Rect(10,10,150,50),"I cant script"))
    15.       {
    16.          // do something...
    17.       }
    18.    }
    19.    toggleTxt = GUI.Toggle(Rect(10, 10, 50, 50), toggleTxt, "A Toggle text");
    20. }
    21.  
    Alternatively, you can use a toggle, if that button only serves the purpose of turning on the next button.

    Code (csharp):
    1.  
    2. private var toggleTxt : boolean = false;
    3. private var showNextButton : boolean = false;
    4.  
    5. function OnGUI()
    6. {
    7.    showNextButton = GUI.Toggle(Rect(10,70,125,50), showNextButton, "Voice Chat On", GUI.skin.button);
    8.  
    9.    if (showNextButton)
    10.    {
    11.       if (GUI.Button(Rect(10,10,150,50),"I cant script"))
    12.       {
    13.          // do something...
    14.       }
    15.    }
    16.    toggleTxt = GUI.Toggle(Rect(10, 10, 50, 50), toggleTxt, "A Toggle text");
    17. }
    18.  
    The reason you need to do this is because the Button will only return true when it is clicked. So the next button can only be shown while the first button is clicked. A normal click is less than a second, so you never actually get to see the next button. By using a unique boolean you can create the behavior you want. Use my first example if you want to make more things happen when that first button is clicked, or the second example for a smaller, neater code.
     
  3. Rastafari

    Rastafari

    Joined:
    Jul 18, 2011
    Posts:
    196
    I love you