- Home /
The question is answered, right answer was accepted
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;
}
}
Can't you just use
indicatorToggle.isOn = !(!toggle1.isOn && !toggle2.isOn && !toggle3.isOn);
?
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
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?
Answer by ItsIcear · Feb 11, 2016 at 11:00 AM
if (!toggle && !toggle2 && !toggle3 && !toggle4)
{
// Function
}
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;
}
}
}
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;
}
}
}
@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.
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;
}
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.
Answer by Charmind · Feb 11, 2016 at 02:03 PM
Use &&, this is boolean function that returns true if all inputs are true.
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 ;)
As I thought, your first description wasnt very clear :D
@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. ;)
Well, glad to be of help, anyway :) If you decide to go that way, dont forget to mark the answer ;)