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())
{
...
}
↧