- Home /
How do you Manage Multiple Boolean Better, I know I'm supposed to use a foreach but I have no idea how that works
Noob Coder here, I'm wondering how to be able to manage multiple Boolean variables, switching some on and off while also being able to add more booleans, I think I'm supposed to use a foreach or I'm supposed to not even use booleans? Any sort of help is appreciated
public class AbilityManager : MonoBehaviour
{
public bool[] Abilities;
// Start is called before the first frame update
void Update()
{
if(Abilities[0] == true)
{
Abilities[1] = false;
Abilities[2] = false;
Abilities[3] = false;
Abilities[4] = false;
}
if (Abilities[1] == true)
{
Abilities[0] = false;
Abilities[2] = false;
Abilities[3] = false;
Abilities[4] = false;
}
}
}
Answer by Martin_Anderson · Jan 28 at 07:05 PM
I suspect that an Enum would work a lot better in your case, as long as I'm correct in assuming that you only ever want one ability to be active at a time.
public enum Abilities {
none,
ability0,
ability1,
ability2,
ability3,
ability4
}
public Abilities currentAbility = Abilities.none;
private void Start() {
currentAbility = Abilities.ability0;
}
So in this code, the Abilities enum defines each of the options that it can be set to. A variable called currentAbility of that Abilities variable type that we just created is then made and set it to none. That currentAbility variable can then be set to any of the abilities.
I hope this helps, and let me know if you encounter any issues or don't understand something