Hello comm unity )))
I got some problems with boolean array.
I need to check if ALL at once booleans are true or false, not exact one but ALL.
So, I got code:
public bool isOpen;
bool allFalse;
for (int i = 0; i < isOpen.Length; ++i)
{
if (isOpen *== true)*
{
allFalse = false;
Debug.Log(“false”);
break;
}
else
{
allFalse = true;
Debug.Log(“true”);
}
}
But it set allFalse = false for each isOpen = false.
And I need to allFalse = false if ALL isopen = false.
I changed a bit from Thajacoth and it worked for me like this
might be a bit too late but for the others
For all true:
bool allTrue = false;
foreach (bool b in boolContainer)
{
if (b)
{
allTrue = true;
}
else
{
allTrue = false;
break;
}
}
if(allTrue)
{
Debug.Log("AllTrue");
}
For all false:
bool allTrue = true;
foreach (bool b in boolContainer)
{
if (b)
{
allTrue = false;
}
else
{
allTrue = true;
break;
}
}
if(!allTrue)
{
Debug.Log("AllFalse");
}
You need to pull your false case out of the loop, like this:
allFalse = false;
foreach(bool b in isOpen)
{
if (b)
{
allFalse = true;
break;
}
}
Debug.Log(allFalse ? "true" : "false");
join this to script and have some fun coding
using System.Linq;
…
//returns true if all elements in array is true
private bool AllArrayInIsTrue(bool Array)
{
return Array.OfType().ToList().TrueForAll(x => x);
}