- Home /
How to disable a bool switch under certain circumstances?
Hey everybody,
For days I am looking how to solve this, without any success. I am trying to switch Sound on and off with a bool var. This actually works quit well. The problem is that I have four buttons that I can combine for different actions. So in my case, I would press button2 to enable certain possibilities . When I keep it pressed and now press button1 I can switch my sound on(or off(via bool)). That works well. If the sound is playing and I now press button3 I enable a Nightmode, that still works fine. But if I press button1 again and enable a "Night Action" the Soundbool turns on and off. But the sound should continue. The other way round works perfect. So if I enable the Nightmode first and then trigger the Night Action the sound keeps disabled. So I somehow need to disable the bool for the first possibility. This is my script which is attached to a master script:
using UnityEngine;
using System.Collections;
public class KlassikScript : MonoBehaviour {
public Masterscript masterParent;
public AudioClip klassiko;
void Start () {
}
// Update is called once per frame
void Update () {
if (masterParent.Song1Enabled == false){
audio.Play();
audio.loop = true;
}
if(masterParent.NachtErst == true){
masterParent.Song1Enabled =false;
audio.Stop();
}
}
}
This is the part from the Masterscript:
else if(pinValue1 == 1 && pinValue2 ==1 && pinValue3 == 0 && pinValue4 == 0){
if(zweiterButtonZuerst == true){ //if button2 pressed first
Song1Enabled = !Song1Enabled;
}
}
I hope anybody has an idea what I might add?! Since it is a part of my final exam I really would appreciate your help!!! :-) Thanks!
Does each bool really need to rely on the others? From the looks of what you're trying to do they should all be independent allowing you to use individual if statements without any && operators.
Answer by Owen-Reynolds · Jul 06, 2014 at 03:00 PM
If there are three buttons, there are 8 combinations of on/off. Just list them all, on paper, and figure what settings do what. Then code && and || to solve it. Any decent 1st semester programming textbook will go into more detail.
Suppose you need A and B pressed, and C not pressed, to do something. Write: if(A && B && !C)
.
I'm using !B instead of B==false, since it does read easier. You'd pronounce if(A && !B)
as "if A and not B", which is shorter as just as good as "if A is true and B is false."
Suppose A does something, unless B and C are both pressed. Then use if(A && !(B && C))
. If that logic is too much, list the combinations by hand: if(A&&!B&&!C || A&&!B&&C || A&&B&&!C)
Your answer
![](https://koobas.hobune.stream/wayback/20220613153349im_/https://answers.unity.com/themes/thub/images/avi.jpg)