- Home /
iterate through array of objects with lists of objects
I need to iterate through an array of objects, each of these objects holds multiple generic Lists of objects. The purpose is to compare a specific object with the contents of the generic lists and to remove it from the 1 list it exists in (it really only does exist in 1 list).
The problem is, when i try to remove the object from the list in question, it removes each object of the same type (by Tag) from each array object's list, and not just the "one".
Each object in the arrColum has a script called Check, which contains multiple generic lists of objects.
This is the script is use to iterate through my array of objects with Lists:
var arrColum = new Transform[8];
function RemoveFromColum(theObject : GameObject)
{
for (var colum : Transform in arrColum)
{
if(colum != null && colum.gameObject.active ==true)
{
var checkScript : Check = colum.gameObject.GetComponent(Check);
if(theObject.transform.tag == "Cube")
{
var theCubeList : List.<GameObject> = checkScript.cubeList;
RemoveFromList(theCubeList, theObject, colum.gameObject);
}
}
}
}
function RemoveFromList (theList : List.<GameObject>, theObject : GameObject, theColum : GameObject)
{
var index : int = -1;
var checkScript : Check = theColum.GetComponent(Check);
for (var curr : GameObject in theList)
{
++index;
if (curr.transform.GetInstanceID() == theObject.transform.GetInstanceID())
break;
}
if( index >= 0 && index < theList.Count)
{
theList.RemoveAt(index);
checkScript.columCount-=1;
}
}
Can anyone point out what I'm doing wrong here. Or suggest a better/quicker way of achieving this properly?
Don't see anything obviously wrong -- add a bunch of Debug: "start remove" "remFromList#2", "remObjID45672", "doneRem". I'm wondering maybe if someone else isn't just calling this for lots of objects.
As far as the structure, depending on what you are doing: if every item can be in only one list, you could have the objects responsible for knowing who owns them.
That's exactly what i ended up doing. Creating a breadcrumb system that i could query to see which array object "owned" the object in question and then only iterate through the appropriate List in that said owner object.
Still perplexing why it would remove multiple objects even after using GetInstanceID to compare to make sure only the one was removed. I'm not sold on GetInstanceID consistently providing a truly unique ID...
Print out each list, using InstanceIDs, just before a remove.