While Loop with Array

Hi,

i’ve been trying to filter through a list of gameobjects in a while loop and removing them if they dont fulfill the condition. e.g:

while (myArray.length > 0) {
myGameObject = myArray.Shift();

//perform other checks with myGameObject

}

am i right in thinking that this should empty the array list? im not running this code in update() by the way. the code seems to go back into the loop even when the array length is 0?? this causes unity to crash.

Any ideas will be very greatly appreciated, really can’t get my head around why this is happening. i’ll try and add an exapmle of the code later today.

Assuming you have a List myArray declared

You have to read the list in the reverse order if you want to remove items from it.

for(int i = myArray.Count -1; i >= 0; i–)
{
Class myItem = myArray*;*
if( myItem.Name != “ImTrue”) // There is the code to your condition
{
MyArray.RemoteAt(i);
}
}

thanks for replying.

I have solved the problem by rewriting my code using a while loop as above. there must have been a condition that was stopping the Shift() function being used hence the array always had more than 0.

Thanks for the advice anyway though!