Ask coding questions

← Back to all posts
What does this error mean?
h
CosmoCat19 (8)

Whenever the laser hits an enemy (use space to launch), the whole program breaks with a weird error. Can someone help me figure out what the error means?

Answered by SixBeeps (5235) [earned 5 cycles]
View Answer
Comments
hotnewtop
SixBeeps (5235)

System.InvalidOperationException: Collection was modified; enumeration operation may not execute.

This likely means you added or removed an item of a list you were iterating over with a foreach loop. I think it's the one at line 98 where you call enemies.RemoveAt() in the foreach loop. This is illegal, so just replace it with a standard for loop.