I need to search for a gameObject in a list and return the index of the first matching gameObject.
I was able to do this using tags on the objects and comparing the tags, but it seems to work for a while and then it starts missing items in the list. The first few times around it will work fine. Then it will say that it didn’t find a copy of the item in the list, when at the time there is indeed a copy, sometimes multiple copies, of the item in the list.
After many hours of debugging I’ve boiled the problem down to the code below. I can’t see any reason it wouldn’t behave just as I intended, and as it seems to at first, but maybe I’m missing something.
So two questions:
1, is there a better way of finding the index of the first matching item in a list and if so what is it?
2, is there anything wrong with the code below that maybe I’m overlooking?
private var checkPool; //For storing the pool list variable
checkPool = objectPool.GetComponent(PoolList).objectList; //Add the pools object list to a variable
var foundObject : boolean = false; //Create a boolean for the upcoming loop
while (foundObject == false) { //While the object hasn't been found
for (var i : int = 0; i <= checkPool.Count - 1; i++) { //Loop through the pool list
if (checkPool[i].tag == myObject.tag) { //If there is a copy of the gameObject in the pool
//Debug.Log ("The pool contained a copy of " + myObject.tag);
foundObject = true; //Flag that the object has been found
return; //Exit the function
}
else { //If there is not a copy of the gameObject in the pool
Debug.Log ("The pool did not contain a copy of " + myObject.tag);
foundObject = true; //Flag that the object has been found
return; //Exit the function
}
}
}