- Home /
C# Using Lists and RemoveAt
I'm doing something wrong but as this is my first time working with Lists, I don't know what. My MobSpawner does this
private void SpawnAllMobs() {
for(int mobcnt = 0; mobcnt < mobCount; mobcnt ++) {
GameObject mob = Instantiate(mobPrefabs[Random.Range(0,mobPrefabs.Length)],
transform.position, Quaternion.identity) as GameObject;
// Get rid of (Clone), setup Parent
mob.gameObject.name = mob.gameObject.name.Replace("(Clone)","").Trim();
mob.transform.parent = transform;
mob.gameObject.GetComponentInChildren<QM_EnemyHealth>().myIndex = mobcnt;
mobs.Add(mob);
}
}
and I see my bad guys have a myIndex value of 0,1,2 (where mobCount is 3) - all good there. When bad guy dies it goes into this:
public void RemoveAMob(int listIndex) {
mobs.RemoveAt(listIndex);
}
the myIndex value is passed to listIndex. From that, I'm getting an error Argument is out of range Parameter name: index but not every time. If I kill mob(0) and do this RemoveAt(0), does something happen to mob(1) and (2) in the List?
http://wiki.unity3d.com/index.php?title=Which_Kind_Of_Array_Or_Collection_Should_I_Use?
Answer by syclamoth · Aug 31, 2013 at 06:43 AM
Short answer: yes, when you remove a mob from the list the indices of the rest of the mobs will get messed up because the list is now shorter! Remember, a list is not an array. When you remove an object from the list, it automatically shortens the list.
Why not make things easier for yourself, and let the list manage it for you? Immediately before you destroy the mob, use
mobs.Remove(curMob);
so that you don't have to worry about keeping track of individual indices.
Better yet, go one step further and use a HashSet ins$$anonymous$$d. If every mob instance is unique, and you don't have to worry about ordering, it will make many common operations much faster.
This sounds good - cur$$anonymous$$ob (gameObject) shares the same name as all the other mobs in the List, can I assume the List uses a pointer or such to know which gameObject is meant for removal?
I see through testing that it does not rely on go.name and my mobspawner is now working, thanks syclamoth. I'm new enough to program$$anonymous$$g in general to say that after reading this http://www.dotnetperls.com/hashset I was sufficient lost and put that on my ToLearn list.
Huh, that page you linked sure looked needlessly complicated! It's really not as hard as it looks, all you really have to do is replace 'List' with 'HashSet', and all the rest of the functionality is the same. All the function calls you need to access a list are the same for a hashset, unless you are trying to manipulate several collections at once (searching for overlapping values, etc), which you are not.
Rule of thumb: hashsets are, in the majority of cases where you are dealing with a group of unique agents, categorically more useful than lists.