- Home /
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;
}
}
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".