diff --git a/src/Collections/IndexableSet.cs b/src/Collections/IndexableSet.cs index d49c597..140f9f5 100644 --- a/src/Collections/IndexableSet.cs +++ b/src/Collections/IndexableSet.cs @@ -61,10 +61,14 @@ namespace MoonTools.ECS.Collections return false; } - var lastElement = array[Count - 1]; var index = indices[element]; - array[index] = lastElement; - indices[lastElement] = index; + + if (index != Count - 1) + { + var lastElement = array[Count - 1]; + array[index] = lastElement; + indices[lastElement] = index; + } count -= 1; indices.Remove(element);