- Home /
 
C# - Another GUI.button in a GUI.button.
So I here's what I want to do. 
I click a button to show 'Main Menu'.(It worked.)
Then there will be 3 buttons in Main Menu(It worked.)
I click 'config 1' then show the player another menu.(IT DIDN'T WORK !)
I've benn working on this for an hour but still can't figure it out,need help here,thanks.
So here's my code.
 using UnityEngine;
 using System.Collections;
 
 public class ButtonOption : MonoBehaviour    {
 
     public MainIsactive = false;
     
     void OnMouseUpAsButton() 
     {
         if ( MainIsactive == false )
         {
             MainIsactive = true;
         }
     }
     
     void OnGUI() 
     {
         if ( MainIsactive == true )
         {
             {
                 GUI.Box (new Rect(500,280,400,400),"Title");
             
                 if(GUI.Button(new Rect(865,290,23,23), "X"))
                 {
                     MainIsactive = false;
                 }
             
                 if(GUI.Button(new Rect(650,410,100,50),"Config 1"))
                 {
                     GUI.Box (new Rect(500,280,400,400),"Title2");
                     GUI.Label ( new Rect(615,315,200,200),"Texttt");
                 }
             
                 if(GUI.Button(new Rect(650,500,100,50),"Config 2"))
                 {
                     //Do Something. 
                 }
             
                 if(GUI.Button(new Rect(650,590,100,50),"Config 3"))
                 {
                     //Do Something. 
                 }
                 
             }
                 
         }
     }
 }
 
               I attached this script to option button.
Answer by Landern · Jul 10, 2014 at 10:33 AM
The problem that you're having is similar to why you have a variable(which i don't know how you're not getting a build error when you declare MainIsactive given it doesn't have a type(bool) specified). When you click on "Config 1", for a frame or two you may get the box and label from your if statement, might appear for a blink of an eye, but then it's depending on a mouse click on "Config 1" to make it appear. Look at your code, you're stating, IF Config 1 button is pressed, show what's in this IF statement. Thats fine and dandy for a millisecond or two, but then the button is no longer pressed. You need to either have a little state system, say using a switch case statement and a string, the string will contain the current menu you're on, in the switch statement, use the case that corresponds with that menu and detail the GUI controls you want to appear. OnGUI can be called multiple times per frame.
 using UnityEngine;
 using System.Collections;
  
 public class ButtonOption : MonoBehaviour   {
  
     public MainIsactive = false;
     private string currentMenu = string.Empty;
     
     void OnMouseUpAsButton() 
     {
         if ( MainIsactive == false )
         {
             MainIsactive = true;
         }
     }
  
     void OnGUI() 
     {
         if ( MainIsactive == true )
         {
             GUI.Box (new Rect(500,280,400,400),"Title");
     
             if(GUI.Button(new Rect(865,290,23,23), "X"))
             {
                 MainIsactive = false;
             }
             
             switch (currentMenu)
             {
                 case "config1":
                 {
                     GUI.Box (new Rect(500,280,400,400),"Title2");
                     GUI.Label ( new Rect(615,315,200,200),"Texttt");
                 }
                 break;
                 case "config2":
                 {
                 }
                 break;
                 case "config3":
                 {
                 }
                 break;
                 default:
                 {
                     if(GUI.Button(new Rect(650,410,100,50),"Config 1"))
                     {
                         currentMenu = "config1";
                     }
             
                     if(GUI.Button(new Rect(650,500,100,50),"Config 2"))
                     {
                         currentMenu = "config2";
                     }
             
                     if(GUI.Button(new Rect(650,590,100,50),"Config 3"))
                     {
                         currentMenu = "config3";
                     }
                 }                
             }
         }
     }
 }
 
               for extra points, use an enum structure for your menu's.
Thanks man! You're amazing!
Sorry for the 'bool',but I did add it in my script in Unity.
The problem is solved,thank again.
Answer by Nerevar · Jul 10, 2014 at 10:38 AM
Hello,
You display your GUI elements only when the button is clicked
try this :
 using UnityEngine;
 using System.Collections;
  
 public class ButtonOption : MonoBehaviour   {
  
     public MainIsactive = false;
     bool showConfig1 = false;
  
     void OnMouseUpAsButton() 
     {
         if ( MainIsactive == false )
         {
             MainIsactive = true;
         }
     }
  
     void OnGUI() 
     {
         if ( MainIsactive == true )
         {
                 GUI.Box (new Rect(500,280,400,400),"Title");
  
                 if(GUI.Button(new Rect(865,290,23,23), "X"))
                 {
                     MainIsactive = false;
                     showConfig1 = false;
                 }
  
                 if(GUI.Button(new Rect(650,410,100,50),"Config 1"))
                 {
                     showConfig1 = true;
                 }
  
                 if(GUI.Button(new Rect(650,500,100,50),"Config 2"))
                 {
                     showConfig1 = false;
                     //Do Something. 
                 }
  
                 if(GUI.Button(new Rect(650,590,100,50),"Config 3"))
                 {
                     showConfig1 = false;
                     //Do Something. 
                 }
  
                 if(showConfig1)
                 {
                        GUI.Box (new Rect(500,280,400,400),"Title2");
                        GUI.Label ( new Rect(615,315,200,200),"Texttt");
                 }
  
         }
     }
 }
 
               you can use this method to display your other configs, I think there are many other possibilities for that too.
cheers
Thanks man.
This is an 'easy to understand' version.
Thank again.
You are welcome, Landerns version would be better if you have a really complicated $$anonymous$$enu, it is a good general solution.
Your answer
 
             Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Making a bubble level (not a game but work tool) 1 Answer
Hold Down GUIButton? 1 Answer
An OS design issue: File types associated with their appropriate programs 1 Answer