Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by editorer · Jul 10, 2014 at 10:26 AM · c#gui.button

C# - Another GUI.button in a GUI.button.

So I here's what I want to do. alt text

  1. I click a button to show 'Main Menu'.(It worked.)

  2. Then there will be 3 buttons in Main Menu(It worked.)

  3. 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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image editorer · Jul 10, 2014 at 10:42 AM 1
Share

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.

avatar image
0

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

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image editorer · Jul 10, 2014 at 10:44 AM 1
Share

Thanks man.

This is an 'easy to understand' version.

Thank again.

avatar image Nerevar · Jul 10, 2014 at 10:52 AM 1
Share

You are welcome, Landerns version would be better if you have a really complicated $$anonymous$$enu, it is a good general solution.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges