- Home /
Question by
AlexGan001 · Jul 28, 2015 at 08:52 AM ·
arraybooleans
Finding the amount of true booleans - Arrays
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.
Comment
Best Answer
Answer by YoungDeveloper · Jul 28, 2015 at 08:57 AM
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[i] == flag) value++;
}
return value;
}