So i’ve got a pool of objects that I am pulling from to place on the screen as the character scrolls along. I also have an array that contains all of these objects, say 20 of the exact same object. I place the objects by finding out whether they are active or not (GameObject.active = false/true). If it’s false its good to be placed, if its true it cant be placed. I’m using a for loop to iterate through the array and find the first available non-active object, but the problem is that i can’t break out of the loop once i find it.
For example:
For(i = 0; i < objects.length; i++)
{
if (objects*.active == false)[/INDENT]*
{[/COLOR]
objects*.transform.position = Vector2(xCoord, yCoord);[/INDENT][/INDENT]*
objects*.activate = true;[/INDENT][/INDENT]*
break; // available object found, break out of for loop.[/COLOR]
}
}
So, i want that break statement to break me out of the for loop, but it seems it is only breaking the if statement because it is placing 20 objects on the screen (iterating through the entire length of the array). Furthermore, i cant put the break statement outside of the if statement, because if i do that and objects happens to not be active = true then no object gets placed.
I dont know if there is a simpler way to iterate, find what your looking for, then stop the loop or not. It seems to be such a simple problem but its driving me nuts lol! Code coma at this point haha. Hope i was clear enough.
Thanks for all the help in advance
,
-Aaron
you can just make the i = 20 or more or i = objects.length + 1, and it will not pass again trough the for loop since it’s condition is not valid.
Thanks for the reply 
Well the thing is that I dont want it to iterate through the whole array. I only want it to loop as many times as it needs to make the if statement true. Once that condition is met and the actions performed it needs to break out. If i make i = 20 then it will iterate 20 times through even if it finds what it needs on the 2nd pass. i believe anyways.
Thanks again! 
-Aaron
I guess none of the objects are inactive? Because break used like that will in fact break out of the loop.
function Start () {
for (i = 0; i < 20; i++) {
if (i == 5) {
break;
}
Debug.Log (i);
}
}
No, if you made i = 20 instead of break, it will accomplish much the same thing.
–Eric
I was referring to this:
for(i = 0; i < objects.length; i++){
if (objects[i].active == false){
objects[i].transform.position = Vector2(xCoord, yCoord);
objects[i].activate = true;
//instead of brake use next line:
i = objects.length; //this way it will get out of the for loop
}
}
Ohhh, make i = to 20 in the if statement, i was thinking he meant in the For declaration line. That might work. As to the other point, they for sure are all inactive because i instantiate and deactivate the entire pool in a start function. They also show up as inactive in the heirachy and are invisible on screen.
this is my exact code (without the i=20 thing yet):
for (var b = 0; b < multiBlocks.length; b++)
{
if (multiBlocks[b].active == false)
{
multiBlocks[b].transform.position = Vector2(copyArray[0], blockSpawnCheckUp + ySpawn);
multiBlocks[b].active = true;
copyArray.RemoveAt(0);
break;
}
}
Famous last words 
Setting b to a number bigger than multiblocks.length will work (albiet messy).
Using break will work.
How about you find you what is happening?
for (var b = 0; b < multiBlocks.length; b++)
{
debug.log(b);
debug.log(multiBlocks[b].active);
if (multiBlocks[b].active == false)
{
multiBlocks[b].transform.position = Vector2(copyArray[0], blockSpawnCheckUp + ySpawn);
multiBlocks[b].active = true;
copyArray.RemoveAt(0);
break;
}
}
Ya i was just playing around with it and its looking like the break might be a few lines up. I’m going to check it out and i will get back to you guys.
Thanks for all the replies and the sound answer that break in an if statement WILL take me out of a for loop haha 
-Aaron
Ya im an idiot. The problem in the code was how it was placing the blocks on the y axis. it was placing them all on the same y-coord and that gave the appearance that it was continually iterating through since it would use all of the pool on that one y-coord. Bleh. I think i need a break. haha, thanks again guys for all the help. if nothing else i have two solid ways to break for statements :).
Thanks again,
Aaron