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 Grove121 · Jun 13, 2012 at 02:25 PM · buttonbooleanswitch

Controlling multiple booleans

Ok I have five switches in a my scene that all need to work in relation to each other. Basically when any one switch is flipped up (on) the other four will be flipped down (off). Ive got this to work going from right to left. For example switch four turns off five, three turns off four, etc. But they don't work from left to right. I'm assuming its because once the update functions sees that switch1 is true, it has no need to search the rest of the function. Maybe i'm wrong though. Either way I cant seem to find a way for this process to work both ways.

 var switch1Obj : GameObject;
 
 private var switch1Script : SwitchAnimations;
 
 
 var switch2Obj : GameObject;
 
 private var switch2Script : SwitchAnimations;
 
 
 var switch3Obj : GameObject;
 
 private var switch3Script : SwitchAnimations;
 
 
 var switch4Obj : GameObject;
 
 private var switch4Script : SwitchAnimations;
 
 
 var switch5Obj : GameObject;
    
 private var switch5Script : SwitchAnimations;
 
 
 
 function Awake (){                                                        
 
 switch1Script = switch1Obj.GetComponent(SwitchAnimations);
 
 switch2Script = switch2Obj.GetComponent(SwitchAnimations);
 
 switch3Script = switch3Obj.GetComponent(SwitchAnimations);
 
 switch4Script = switch4Obj.GetComponent(SwitchAnimations);
 
 switch5Script = switch5Obj.GetComponent(SwitchAnimations);
 
 }
 
 
 
 function Update(){
 
     if (switch1Script.turnedOn == true){
 
         switch5Script.turnedOn = false;
 
         switch4Script.turnedOn = false;
 
         switch3Script.turnedOn = false;
 
         switch2Script.turnedOn = false;
 
     }
 
     if (switch2Script.turnedOn == true){
 
         switch3Script.turnedOn = false;
 
         switch4Script.turnedOn = false;
 
         switch5Script.turnedOn = false;
 
         switch1Script.turnedOn = false;
 
     }
 
     if (switch3Script.turnedOn == true){
 
         switch4Script.turnedOn = false;
 
         switch5Script.turnedOn = false;
 
         switch1Script.turnedOn = false;
 
         switch2Script.turnedOn = false;
 
     }
 
     if (switch4Script.turnedOn == true){
 
         switch5Script.turnedOn = false;
 
         switch1Script.turnedOn = false;
 
         switch2Script.turnedOn = false;
 
         switch3Script.turnedOn = false;
 
     }
 
     if (switch5Script.turnedOn == true){
 
         switch1Script.turnedOn = false;
 
         switch2Script.turnedOn = false;
 
         switch3Script.turnedOn = false;
 
         switch4Script.turnedOn = false;
 
     }    
 
 }
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Owen-Reynolds · Jun 13, 2012 at 03:38 PM

Suppose switch #1 was on, the rest off, and the player has just hit switch #4. You know that #4 is the most recent, so should turn off #1, but Update doesn't. It just sees two switches, both up. Since you check for #1 first, it wins and turns off #4.

Change to the way it "really works." Whenever you set turnedOn to true for some switch, right then you also set it to false for every other switch. You might move the entire code into the switch script and replace the if-test with "if(myNumber==1) turn off 2-5".

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Unity UI Button Switch Bool? 2 Answers

How To destroy specific objects? 2 Answers

A bool is equal to the opposite of itself dosen't work 1 Answer

If statement breaking button color copy 0 Answers

On boolean becoming true, do something - how do I stop code activating over and over? 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