Is there an easy way to get a value of the amount of booleans in an array which are true
I am using an bool array and not an array with bools being placed into it.
Is there an easy way to get a value of the amount of booleans in an array which are true
I am using an bool array and not an array with bools being placed into it.
Simply loop through array and count how much are there.
void Start() {
bool[] test = new[] { true, false, false };
int pos = count(test, true);
int neg = count(test, false);
Debug.Log("there are " + pos + " true values");
Debug.Log("there are " + neg + " false values");
}
public static int count(bool[] array, bool flag){
int value = 0;
for(int i = 0; i < array.Length; i++) {
if(array *== flag) value++;*
}
return value;
}