On MSDN you can read this about the List.Remove Method:
If type
T
implements theIEquatable<T>
generic interface, the equality comparer is theEquals
method of that interface; otherwise, the default equality comparer isObject.Equals
.
If neither IEquatable<T>
is implemented by the game object nor Object.Equals
is overridden then reference equality is used for the comparison and this one will never fail if the given object is really in the list. This is the default behavior of Object.Equals
.
I could imagine that either a faulty Object.Equals
override or IEquatable<T>
implementations exists that makes the Remove method fail.
Another possibility is that you try to remove a clone of the game object. In that case reference equality cannot work! You could fix it by overriding the Object.Equals
method and to make it compare object IDs. Or you could remove like this:
int index = aliveMonsters.FindIndex(m => m.ID == monster.ID);
if (index >= 0 ) {
aliveMonsters.RemoveAt(index);
}
But in any case, make sure that there is really a problem with the list and not just with the game logic. The object might be removed from the list but not from the game.