Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
This question was closed Feb 11, 2016 at 01:44 PM by meat5000 for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by KnightRiderGuy · Feb 11, 2016 at 12:19 AM · c#uibuttonstoggletoggle button

Do Something ONLY when all Toggles are On or Off

Is there a way to do something ONLY when all three toggles are either on or Off?

My problem is that I have three toggles that are set up like this, I also have another toggle that serves as an indicator light that I want the light/toggle to go off when the last toggle is turned off but turn On when any of the toggles are turned on.... not sure if that makes sense?

 //Activate Audio Button
      public void SIDaudioButtonOnOff (){
          SIDAudioState = ! SIDAudioState;
          if (SIDAudioState == true) {
              toggleSIDindicator = GameObject.Find ("ToggleMainIndicatorLight").GetComponent<UnityEngine.UI.Toggle> ();
              toggleSIDindicator.isOn = true;
          }else {
              SIDAudioState = false;
  
          }
      }
Comment
Add comment · Show 3
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 saschandroid · Feb 11, 2016 at 12:00 PM 0
Share

Can't you just use

 indicatorToggle.isOn = !(!toggle1.isOn && !toggle2.isOn && !toggle3.isOn);

?

avatar image meat5000 ♦ saschandroid · Feb 11, 2016 at 12:04 PM 0
Share

Thats what I said in my first answer "Use an if statement" but OP presented a more complex scenario.

$$anonymous$$y snippet puts the 'light' on if any switches are pressed then only puts it off if all switches are active then one is turned off.

It could very well be that my interpretation is wrong :D

avatar image saschandroid meat5000 ♦ · Feb 11, 2016 at 12:17 PM 0
Share

This should do exactly what the OP wants: if one of the toggles is on, the statement in the () is false and the indicator is set to be !false (true) and if all toggles are off the statement is true and the indicator is set to be !true (false). I don't see any more complex scenario in this. Am I missing something?

4 Replies

  • Sort: 
avatar image
0
Best Answer

Answer by ItsIcear · Feb 11, 2016 at 11:00 AM

 if (!toggle && !toggle2 && !toggle3 && !toggle4)
 {
       // Function
 }
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 KnightRiderGuy · Feb 11, 2016 at 01:23 PM 0
Share

Right on, based on what looked simplest This seemed a logical choice although I am very intrigued by the method suggested by @meat5000, too. This was what I did with my code and it seems to work great, if any of the toggle are on then my top indicator toggle is active but if all are off then the top indicator is also off, just what I needed.

I do need to make a kind of auto activate function so that when "Auto" is set the Toggles for "Audio, Video & Heat Sensor" will activate in a times sequence the first one being the "Activate/Deactivate" indicator toggle.... I'm wondering if @meat5000 method in the end would be a better choice for that?

  //Toggle Objects
     public UnityEngine.UI.Toggle toggleSIDindicator; //$$anonymous$$ain Indicator Light Toggle
     public UnityEngine.UI.Toggle toggleSIDaudio; //Audio Toggle
     public UnityEngine.UI.Toggle toggleSIDvideo; //Video Toggle
     public UnityEngine.UI.Toggle toggleSIDheatSensor; //Heat Sensor Toggle
 
     //Device State Settings
     public bool SIDAudioState = false; //Audio Toggle
     public bool SIDVideoState = false; //Video Toggle
     public bool SIDHeatState = false; //Heat Sensor Toggle
 
     
         //Activate Audio Button
         public void SIDaudioButtonOnOff (){
             SIDAudioState = ! SIDAudioState;
             if (SIDAudioState == true) {
                 toggleSIDindicator = GameObject.Find ("Toggle$$anonymous$$ainIndicatorLight").GetComponent<UnityEngine.UI.Toggle> ();
                 toggleSIDindicator.isOn = true;
             }else {
                 if (! SIDAudioState && ! SIDVideoState && ! SIDHeatState){
                 toggleSIDindicator = GameObject.Find ("Toggle$$anonymous$$ainIndicatorLight").GetComponent<UnityEngine.UI.Toggle> ();
                 toggleSIDindicator.isOn = false;
                 }
             }
         }
     
         //Activate Video Button
         public void SIDvideoButtonOnOff (){
             SIDVideoState = ! SIDVideoState;
             if (SIDVideoState == true) {
                 toggleSIDindicator = GameObject.Find ("Toggle$$anonymous$$ainIndicatorLight").GetComponent<UnityEngine.UI.Toggle> ();
                 toggleSIDindicator.isOn = true;
             }else {
                 if (!SIDAudioState && !SIDVideoState && !SIDHeatState) {
                     toggleSIDindicator = GameObject.Find ("Toggle$$anonymous$$ainIndicatorLight").GetComponent<UnityEngine.UI.Toggle> ();
                     toggleSIDindicator.isOn = false;
                 }
             }
         }
     
         //Activate Heat Sensor Button
         public void SIDheatSensorButtonOnOff (){
             SIDHeatState = ! SIDHeatState;
             if (SIDHeatState == true) {
                 toggleSIDindicator = GameObject.Find ("Toggle$$anonymous$$ainIndicatorLight").GetComponent<UnityEngine.UI.Toggle> ();
                 toggleSIDindicator.isOn = true;
             }else {
                 if (!SIDAudioState && !SIDVideoState && !SIDHeatState) {
                     toggleSIDindicator = GameObject.Find ("Toggle$$anonymous$$ainIndicatorLight").GetComponent<UnityEngine.UI.Toggle> ();
                     toggleSIDindicator.isOn = false;
                 }
             }
         }

 
 
 

avatar image
1

Answer by meat5000 · Feb 11, 2016 at 12:20 AM

Yes. Use an if statement. I used int instead of bools. Seemed easier.

  using UnityEngine;
  using System.Collections;
  
  public class ToggleTest : MonoBehaviour {
  
      public int sw1 = 0;
      public int sw2 = 0;
      public int sw3 = 0;
      public bool indicator = false;
      public int lastCount = 0;
      public int count = 0;
      
      // Update is called once per frame
      void Update () {
      
          if(lastCount == 3 && count == 2 && indicator == true)
          {
              indicator = false;
          }
          else
          {
              if(count == 3)
              {
                  indicator = true;
                  lastCount = count;
              }
              else if(count > 0 && lastCount != 3)
              {
                  lastCount = count;    
              
                  
                  indicator = true;
              }
              else if(count == 0)
              {
                  indicator = false;
                  lastCount = count;
              }
  
              count = sw1 + sw2 + sw3;
          }
      }
  }
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 KnightRiderGuy · Feb 11, 2016 at 12:45 AM 0
Share

@meat5000, O$$anonymous$$ lets say I have a 4th toggle that is set at Not Intractable, This toggle is just an indicator.

Now lets say the the other toggles need to be ALL Off in order for the "Indicator Toggle" to be off also.

with me so far?

Now lets also say that you can turn any of the toggles on and the indicator toggle will go on.

But will ONLY go OFF if all of the toggles are off.

avatar image meat5000 ♦ KnightRiderGuy · Feb 11, 2016 at 01:17 AM 0
Share

Solved it by counting ints. I'm guessing you'll look at the code and find a way to make it more elegant. Its crude but it works.

Just like binary, 0 is off, 1 is on.

Add the script to an object and put 1/0 in the inspector to change the fields on or off. Play around with it, changing 1 to 0 and back in all combinations to make sure it holds up.

$$anonymous$$ake a little if statement to convert between bool and int. Put it in a function so you can use it on anything.

 int BoolToInt(bool myBool)
 {
      if(myBool == false)
           return 0;
      else return 1;
 }
 
 bool IntToBool(int myInt)
 {
      if(myInt == 0)
           return false;
      else return true;
 }
avatar image meat5000 ♦ KnightRiderGuy · Feb 11, 2016 at 02:05 AM 0
Share

I went by your first description. All off, indicator off. Any on, indicator on. When all on, one off, indicator off.

Reading again, it may not be what you wanted but the all on, all off scenario is much simpler.

avatar image
1

Answer by Charmind · Feb 11, 2016 at 02:03 PM

Use &&, this is boolean function that returns true if all inputs are true.

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 KnightRiderGuy · Feb 11, 2016 at 12:43 PM

Wow Thanks @meat5000, @ItsIcear, & @saschandroid, I was searching all over the place for a way to do this, this gives me a lot to try out, I'll see what I come up with today and report back the results.

But just to re cap a little the indicator toggle is just to signal that any of the toggle buttons are ON, That way it means the device is active in some function, by either one or all of the 3 toggle buttons.

BUT if ONLY say one of the toggles is off and the other two are ON then the indicator toggle would need to be ON because it would mean the device is active in some way governed by the 2 toggle being ON.

Now if all toggle are OFF the the device is NOT active in any way so the "Indicator" toggle would then be off.

In my attachment you can see at the top I have an indicator that says Activate/Deactivate and on the bottom I have 3 buttons for "Audio" "Video" and "Heat Sensor" It is these 3 buttons that govern the top indicator being on..... I think you guys are on the right track with what I'm trying to do though ;) I'll see what I come up with ;)

Screen Cap of My SID Activation Screen


12687853-1053197948035448-131999154987580753-n.jpg (112.9 kB)
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 meat5000 ♦ · Feb 11, 2016 at 01:44 PM 1
Share

As I thought, your first description wasnt very clear :D

avatar image KnightRiderGuy meat5000 ♦ · Feb 11, 2016 at 02:16 PM 0
Share

@meat5000 thanks man, I thought you had got it pretty good. Like I say I think your method might end up being the way to go for when I try and do an auto activate function set to a timer so that all of the buttons activate in sequence from left to right. ;)

avatar image meat5000 ♦ KnightRiderGuy · Feb 11, 2016 at 02:52 PM 1
Share

Well, glad to be of help, anyway :) If you decide to go that way, dont forget to mark the answer ;)

Follow this Question

Answers Answers and Comments

94 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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

Toggles/Toggle Groups Question 0 Answers

Offsetting RectTransform based on button state button 1 Answer

Use a toggle button to change a GameObjects visibility 2 Answers

How convert this Door Script to work with Ui Buttons! 1 Answer

How do i detect if toggle has been unchecked 0 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