Check boolean Array is all true

Hi to All .

How can i check a boolean array is all true or all false without make a code like this ?

var myArray : boolean[3];

function Start()
{
    if(myArray[0] == false  myArray[1] == false  myArray[2] == false ) print ("all bool are false") ;
}

This code is functional for small arrays, but if my array length is of 100 or an indefinite length … how can i do ?

Thank You

Something like this (note - pseudo code - re-write in your favourite language!)

bool AllTrue=true;
for(int loop=0; loop<array.length;++loop)
{
   if(array[loop]=false)
   { 
       AllTrue=false;
       break;
   }
}
//AllTrue will be true unless an array element is false. It will break out early if a false value is found
function AreAllBooleansThisState (array : boolean [], state : boolean)
{
	var answer : boolean = true;
	for (var i : int = 0; i < array.length; i++)
	{
		if (array [i] != state)
		{
			answer = false;
			break;
		}
	}
	return answer;
}

Edit : Noooooooooooooo…!

thanks guys … very fast

Careful, there’s a pretty significant bug in Prodigalpops’ code on this line:
if(array[loop]=false)

You need to have a double-equals sign there otherwise it will overwrite all the values in the loop to false. (unless this is yet another magical quirk of UnityScript…)

(typesafe code from AkilaeTribe? And abstracted into a dedicated method? sniff That’s my chicken! I’m so proud!)

^.^ the chicken is evolving, he is able to lay Kinder Egg with beautiful code lines inside !

But he forgot to add : boolean

function AreAllBooleansThisState (array : boolean [], state : boolean) : boolean

To ensure it returns a boolean ! :slight_smile:

Good catch there FizixMan - I rushed the code a bit (despite my qualification of it being pseudo code)