Skip to main content
added 179 characters in body
Source Link
Yuriy
  • 191
  • 3

Once you remove at index N, the next value in the list will take it place, and it will be skipped in verification, because you increment an index. So firstly

Solution 1. Firstly create a list of items to be removed, and then use it as source for removal.

 List<GameObject> allDoors = doorsLeft.Union(doorsRight).ToList(); List<GameObject> toRemove = new List<GameObject>(); for(int i = 0; i < allDoors.Count; i++) { if(allDoors[i].transform.parent.name != "Wall_Door_Long_01" && allDoors[i].transform.parent.name != "Wall_Door_Long_02") { toRemove.Add(allDoors[i]); } } foreach(var it in toRemove) { allDoors.Remove(it); } 

Solution 2. Right after removing at index N, decrement index variable to check the new element at this index again next loop iteration.

allDoors.RemoveAt(i); i--; 

Once you remove at index N, the next value in the list will take it place, and it will be skipped in verification, because you increment an index. So firstly create a list of items to be removed, and then use it as source for removal.

 List<GameObject> allDoors = doorsLeft.Union(doorsRight).ToList(); List<GameObject> toRemove = new List<GameObject>(); for(int i = 0; i < allDoors.Count; i++) { if(allDoors[i].transform.parent.name != "Wall_Door_Long_01" && allDoors[i].transform.parent.name != "Wall_Door_Long_02") { toRemove.Add(allDoors[i]); } } foreach(var it in toRemove) { allDoors.Remove(it); } 

Once you remove at index N, the next value in the list will take it place, and it will be skipped in verification, because you increment an index.

Solution 1. Firstly create a list of items to be removed, and then use it as source for removal.

 List<GameObject> allDoors = doorsLeft.Union(doorsRight).ToList(); List<GameObject> toRemove = new List<GameObject>(); for(int i = 0; i < allDoors.Count; i++) { if(allDoors[i].transform.parent.name != "Wall_Door_Long_01" && allDoors[i].transform.parent.name != "Wall_Door_Long_02") { toRemove.Add(allDoors[i]); } } foreach(var it in toRemove) { allDoors.Remove(it); } 

Solution 2. Right after removing at index N, decrement index variable to check the new element at this index again next loop iteration.

allDoors.RemoveAt(i); i--; 
Source Link
Yuriy
  • 191
  • 3

Once you remove at index N, the next value in the list will take it place, and it will be skipped in verification, because you increment an index. So firstly create a list of items to be removed, and then use it as source for removal.

 List<GameObject> allDoors = doorsLeft.Union(doorsRight).ToList(); List<GameObject> toRemove = new List<GameObject>(); for(int i = 0; i < allDoors.Count; i++) { if(allDoors[i].transform.parent.name != "Wall_Door_Long_01" && allDoors[i].transform.parent.name != "Wall_Door_Long_02") { toRemove.Add(allDoors[i]); } } foreach(var it in toRemove) { allDoors.Remove(it); }