Quantcast
Channel: Answers for "Problem with ArrayList and remove Gameobject C#"
Viewing all articles
Browse latest Browse all 4

Answer by whydoidoit

$
0
0
Yeah that's a fun one - a foreach can't iterate a list that changes - it breaks the enumerator. Add: var deleteEnemies : ArrayList = new ArrayList(); Just before the foreach. Then rather than allennemys.Remove(go); Do deleteEnemies.Add(go); And after the end of the foreach do another one like this: foreach(var e in deleteEnemies) { allennemys.Remove(e); } If you want a less verbose way - you can just make a copy of the allennemys list using Linq. using System.Linq; .... foreach(GameObject fo in allennemys.ToList()) { ... }

Viewing all articles
Browse latest Browse all 4

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>