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
1
Question by Taran0 · Nov 23, 2010 at 02:11 PM · javascriptbooleantoggle

Two toggles, only one can be on, both can be off

Hi I'm having a problem with creating two toggles where only one can be on, if one is pressed the other switches off.

i am using the following code

    if (toggleVert == true){
toggleBrie = false;
}
if (toggleBrie == true){
toggleVert = false;
}

it seems to work one way if i click on toggleBrie, toggleVert switches off, but if toggleBrie is on toggleVert will not switch on and switch other off.

any help would be appreciated.

taran

Comment
Add comment · Show 1
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 burnumd · Nov 23, 2010 at 02:39 PM 0
Share

A couple questions: how are you changing these values, and are you using C# or JS?

3 Replies

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

Answer by burnumd · Nov 23, 2010 at 02:46 PM

Let's look at the flow of your code. First you check if toggleVert is true. If it is, you change toggleBrie to false. When you get to the next statement "if (toggleBrie == true)" you've either just set toggleBrie to be false or it was not true to begin with. So you'll never fall into that expression. The better way to handle this problem is to determine this logic when one or the other is changed rather than, as I'm guessing you're doing, in Update/OnGUI/etc.

function SetVert (state : boolean) { if (state) { SetBrie (false); } toggleVert = state; }

function SetBrie (state : boolean) { if (state) { SetVert (false); } toggleBrie = state; }

And only use those functions to change the state of those values.

So, if you want to change the value with a GUI toggle element, just send the result of the Toggle function as the parameter.

function OnGUI ()
{
    SetVert (GUILayout.Toggle (toggleVert, "Vert"));
    SetBrie (GUILayout.Toggle (toggleBrie, "Brie"));
}

For C# users, you can use properties to get more natural-looking GUI code.

As a side note, when you're testing against booleans, you can simply test "if (toggleVert)," the "== true" is redundant.

Comment
Add comment · Show 3 · 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 Taran0 · Nov 23, 2010 at 03:11 PM 0
Share

O$$anonymous$$ i understand running a function that takes care of it, but how do I run these functions when a toggle is pressed, as I am having problems updating my scene, the toggle does not seem to update or run anything. (im using javascript)

avatar image burnumd · Nov 23, 2010 at 03:26 PM 0
Share

See updated answer.

avatar image Taran0 · Nov 24, 2010 at 02:08 PM 0
Share

Awesome mate that worked a charm, I would never have thought of doing that , passing the result of the toggle to a function. I clearly have a lot to learn.

avatar image
0

Answer by Slem · Nov 23, 2010 at 02:38 PM

Where do you set this? In GUI? I would set the other value just after setting one. Like so(if set in GUI, but it applies):

   toggleVert = GUILayout.Toggle(toggleVert);
    if(GUI.changed)
    {
    toggleBrie = !toggleVert;
    }
    //and same of toggleBrie below.

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 bedford · Sep 24, 2012 at 09:45 AM

This has been asked a while ago, I know, but I found an easier way than the one proposed and it may help other people looking for a solution (I had many toggles on same group):

 //declaration
 private bool q1a;
 private bool q1b;
 private bool q1c;
 
 void OnGUI(){
   change1a(GUI.Toggle(new Rect(10,10,10,10),q1a,""));
   change1b(GUI.Toggle(new Rect(30,10,10,10),q1b,""));
   change1c(GUI.Toggle(new Rect(50,10,10,10),q1c,""));
 }
 
 private void change1a(bool newvalue){
   if(newvalue){
     q1b = false;
     q1c = false;
   }
   q1a = newvalue;
 }
 
 private void change1b(bool newvalue){
   if(newvalue){
     q1a = false;
     q1c = false;
   }
   q1b = newvalue;
 }
 
 private void change1c(bool newvalue){
   if(newvalue){
     q1a = false;
     q1b = false;
   }
   q1c = newvalue;
 }

Hope this help !

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

2 People are following this question.

avatar image avatar image

Related Questions

Can someone help me fix my Javascript for Flickering Light? 6 Answers

Setting Scroll View Width GUILayout 1 Answer

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

One use per level question 1 Answer

Drilling-game script's logical bug. 3 Answers


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