Question: I posted yesterday asking for help about the collisions between two instances in game, the enemies and bullets.

The solution was:

```
foreach (Enemy enemy in enemies)
    foreach (Bullet bullet in bullets)
        if (bullet.boundingBox.Intersects(enemy.boundingBox)) 
        { 
            score += 1;
        }

```

And of course this worked score wise, as I now gain a point every time a bullet intersects with an enemy. The problem occurs when I need to destroy both objects (not all, just the ones that have intersected). My first thought was to use the removeAt() function like so:

```
if (bullet.boundingBox.Intersects(enemy.boundingBox)) 
    { 
        score += 1;
        removeAt();
    }    

```

But of course this wouldn't work because generally it's used with an index such as "removeAt(i)". I then tried taking the whole chunk of code that I referenced up the top, and placing it inside both lists like so:

```
for (int i = bullets.Count - 1; i >= 0; i--)
{
    bullets[i].Update(delta);   

    //Bullets being destroyed upon leaving 
    if (bullets[i].position.Y < 0)
            bullets.RemoveAt(i);

    foreach (Enemy enemy in enemies)
        foreach (Bullet bullet in bullets)
            if (bullet.boundingBox.Intersects(enemy.boundingBox)) 
            { 
                bullets.RemoveAt(i);
            }
}

```

The game will run at this point, but crash as soon as the bullet collides with the enemy. An error "System.InvalidOperationException" is thrown at the "foreach (Bullet bullet in bullets) line.

I honestly don't know where I should go from here, so any help is appreciated.

Answer: 
The reason for this would be because you are removing elements from `bullets` while you are enumerating over it. See this:

```
for (int i = bullets.Count - 1; i >= 0; i--)

```

You store the count of bullets in i, and decrease it every time. Then you have two places in your code where the logic removes bullets, thus changing the count of `bullets`. You are never supposed to modify an array you are enumerating over.

As to a possible solution, you could do exactly what Jason in the comments pointed out. Add a `bool Delete` property to your class, then instead of calling `RemoveAt`, set the `Delete` flag to true. The code would look like this:

```
for (int i = bullets.Count - 1; i >= 0; i--)
{
    bullets[i].Update(delta);   

    //Bullets being destroyed upon leaving 
    if (bullets[i].position.Y < 0)
    {
        bullets[i].Delete = true;
    }

    foreach (Enemy enemy in enemies)
    {
        if (bullets[i].boundingBox.Intersects(enemy.boundingBox)) 
        { 
            bullets[i].Delete = true;
        }
    }
}

bullets.RemoveAll(b => b.Delete);

```
Often, I prefer to maintain a new collection for objects to be removed, and then remove them after processing is complete. I find it easy to follow and doesn't have the questionable addition of a control property. Similar to this....

```
List<Bullet> expiredBullets = new List<Bullet>();

for (int i = bullets.Count - 1; i >= 0; i--)
{
  bullets[i].Update(delta);

  if (bullets[i].position.Y < 0)
  {
    expiredBullets.Add(bullets[i]);
  }
}

foreach(Bullet expiredBullet in expiredBullets)
{
  bullets.Remove(expiredBullet);
}

```