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 Cbjfan1 · Mar 29, 2013 at 03:06 AM · guimenutoggleenable

Gui Toggle help

I'm working on a main menu. I'm trying to get a bunch of toggle options in the settings, but no matter which one you toggle, it only toggles on and off the first one.

Here is the script: enter code herestatic var Quality : boolean = false; private var displayLabel : boolean; private var toggleTxt : boolean = false; private var toggleTxt1 : boolean = false;

 function Start(){
 
     displayLabel = false;
 
 }
 
 function OnGUI () {
 GUI.skin = customSkin;
 GUI.Box(new Rect(20,20,200,100),"");
 //You can change name for your box (before parentheses & after last comma)//Also you can resize the box by changing the numbers(first two numbers for the position & the last two numbers for size the GUI objects)
 if(GUI.Button(Rect(25,25,70,20),"Join")){
 Debug.Log("You clicked that button");
 }
 if(GUI.Button(Rect(25,50,70,20),"Settings")){
 displayLabel = true;
 }
     if(displayLabel){
 
          GUI.Box(Rect (500,20,200,100),"Settings");
 
     }
     if(displayLabel){
        toggleTxt = GUI.Toggle(Rect(500, 50, 200, 100), toggleTxt, "Fastest");
        if(toggleTxt){
        RenderSettings.ambientLight = Color.red;
        Quality = true;
        }
         else
         {
         toggleTxt = false;
         RenderSettings.ambientLight = Color.clear;
         }
        }
            if(displayLabel){
        toggleTxt1 = GUI.Toggle(Rect(500, 65, 200, 100), toggleTxt1, "Fast");
        if(toggleTxt1){
        RenderSettings.ambientLight = Color.red;
        Quality1 = true;
        }
         else
         {
         toggleTxt1 = false;
         RenderSettings.ambientLight = Color.clear;
         }
        }
 //That's how to create a GUI button//(The same)
 GUI.Label(new Rect(Screen.width - 500,0,100,50), "Task Force-8");
 //You can change name for this label by typing words (before parentheses & after last comma)
 }
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
0

Answer by iwaldrop · Mar 29, 2013 at 04:07 AM

You shouldn't mix logic in with GUI stuff. Setting the flags toggleTxt and toggleTxt1 is all you should be doing in there, then checking the flags in a loop somewhere (Update is doable, but not ideal).

In fact, this is not a very good way of going about it at all. The reason is that you're setting flags instead of kicking off a method to handle the changes. Ideally you have buttons that trigger one method that handles cases of settings, thusly;

 public enum Quality { Low, Med, High };
 public Quality quality = Quality.Med;
 
 void Awake()
 {
     QualityToggle(quality);
 }
 
 void OnGUI()
 {
     if (GUI.Button(someRect, "Low"))
         QualityToggle(Quality.Low);
     if (GUI.Button(someRect, "Medium"))
         QualityToggle(Quality.Med);
 }
 
 void QualityToggle(Quality quality)
 {
     switch (quality)
     {
     case Quality.Low:
         this.quality = quality;
         // do low quality stuff
         break;
     case Quality.Med...
     }
 }
Comment
Add comment · 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
0

Answer by Chronos-L · Mar 29, 2013 at 04:16 AM

I was going to comment on the way you structure your logic, but @iwaldrop got ahead of me. iwaldrop also provided a very good way to define the level of quality, by using a enum.


The reason your toggle is not behaving properly is you have define a unusually large Rect() for the GUI.Toggle()

Change it to from these:

 toggleTxt = GUI.Toggle(Rect(500, 50, 200, 100), toggleTxt, "Fastest");
 ...
 toggleTxt1 = GUI.Toggle(Rect(500, 65, 200, 100), toggleTxt1, "Fast");

To something like these :

 toggleTxt = GUI.Toggle(Rect(500, 50, 200, 25), toggleTxt, "Fastest");
 ...
 toggleTxt1 = GUI.Toggle(Rect(500, 75, 200, 25), toggleTxt1, "Fast");

Pay attention to the position and the size of the Rect(), in your original code, the Rect for the first toggle overlaps the second toggle; you thought you were pressing on the second toggle but you are affecting the first.

Comment
Add comment · 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

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

12 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Background color and structure disappear when enabling GUI Canvas 0 Answers

Re Enable C# script on camera 1 Answer

Close my GUI button by repressing the same Hot-key. 3 Answers

Stand alone player 0 Answers

Make this GUI Bigger? 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