- Home /
How do I check if all objects in an array are destroyed?
Hai everyone! So I was wondering if I could use an array to check if all the gameObjects in that array are destroyed. This is what I tried doing:
for(var i=0;i<Turrets3.Length;i++)
{
if(Turrets3[i] == null)
{
Start3 = true;
}
}
But that made it if I only destroyed one of the enemies attached to the array I have. I don't know if I'm not using the for() thing wrong or if there's another way to check it. ( NOTE: I have never used a for() thing before ) As always your help is appreciated! :D
Answer by Rydrako · Dec 08, 2012 at 03:41 PM
Ok I guess I'm not good for loops but I used JeffShock's concept. So I did something like this:
var i3 = 0;
var aliveCount3 = 0;
function Start ()
{
aliveCount3 = Turrets3.Length;
}
function Update ()
{
if(Turrets3[i3] == null)
{
aliveCount3 -= 1;
i3++;
}
if(aliveCount3==0)
{
Start3 = true;
}
}
It works great!
Answer by JeffShock · Dec 08, 2012 at 04:07 AM
You could do something like this:
int aliveCount = 0;
for(var i=0;i<Turrets3.Length;i++)
{
if(Turrets3[i] != null)
{
aliveCount++
}
}
This basically counts how many elements are still alive. If aliveCount == 0 at the end of the loop, you know that all of the elements are null or dead.
Another possibility:
var Start3: boolean;
Start3 = true; // initialize Start3 to true
for (var gameObj: GameObject in Turrets3){
if (gameObj) Start3 = false; // any turret alive turns Start3 to false
}
I tried both of you guy's answers but it still calls Start3 if only one element is destroyed. $$anonymous$$aybe I'm not doing something right...
Your answer
Follow this Question
Related Questions
Read Array Inside Array 0 Answers
Accessing boolean properties from array of transforms 1 Answer
Array index is out of range 1 Answer
Wrong if command 3 Answers
Problem with nested for loops 2 Answers