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

Hello,
im currently scripting this script and this part is making problems:
foreach(room room in roomsToRemove) {
possibleRooms.Remove(room);
}
Unity displays this Error when I try to run the game:
InvalidOperationException: Collection was modified; enumeration operation may not execute.
System.Collections.Generic.List1+Enumerator[T].MoveNextRare () (at <88e4733ac7bc4ae1b496735e6b83bbd3>:0) System.Collections.Generic.List1+Enumerator[T].MoveNext () (at <88e4733ac7bc4ae1b496735e6b83bbd3>:0)
worldGen.g__Walk|30_0 (worldGen+<>c__DisplayClass30_0& ) (at Assets/scripts/World Gen/worldGen.cs:206)
worldGen.Awake () (at Assets/scripts/World Gen/worldGen.cs:134)

In case you need the whole script, here it is:
I apologize for the comments being in german.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class worldGen : MonoBehaviour
{
//Corridors
[SerializeField]
private GameObject TBCorridorObject;
[SerializeField]
private GameObject LRCorridorObject;
//Rooms
[SerializeField]
private GameObject closedRoomObject;
[SerializeField]
private GameObject LTBROpenedRoomObject;

[SerializeField]
private GameObject ROpenedRoomObject;
[SerializeField]
private GameObject LOpenedRoomObject;
[SerializeField]
private GameObject TOpenedRoomObject;
[SerializeField]
private GameObject BOpenedRoomObject;

[SerializeField]
private GameObject LROpenedRoomObject;
[SerializeField]
private GameObject TBOpenedRoomObject;

[SerializeField]
private GameObject LBOpenedRoomObject;
[SerializeField]
private GameObject RBOpenedRoomObject;
[SerializeField]
private GameObject RTOpenedRoomObject;
[SerializeField]
private GameObject LTOpenedRoomObject;

[SerializeField]
private GameObject LBROpenedRoomObject;
[SerializeField]
private GameObject BRTOpenedRoomObject;
[SerializeField]
private GameObject LTROpenedRoomObject;
[SerializeField]
private GameObject LTBOpenedRoomObject; 

//bools für starterroom Auswahl
private bool bool1 = false;
private bool bool2 = false;
private bool bool3 = false;
private bool bool4 = false;

//Walker
private Vector3 walker;
private int walkCounter = 1;
private bool alreadySet = false;
private bool checkLeft = false;
private bool checkRight = false;
private bool checkTop = false;
private bool checkBottom = false;

private GameObject starterRoomObject;
private void Awake() {

    int starterRoomRnd = Random.Range(1, 4);

    switch (starterRoomRnd) {
        case 1: bool1 = true; starterRoomObject = TOpenedRoomObject; break;
        case 2: bool2 = true; starterRoomObject = BOpenedRoomObject; break;
        case 3: bool3 = true; starterRoomObject = ROpenedRoomObject; break;
        case 4: bool4 = true; starterRoomObject = LOpenedRoomObject; break;
    }

    room starterRoom = new room(bool1, bool2, bool3, bool4, starterRoomObject);

    int arrayLenght = Random.Range(20, 20);

    List<room> rooms = new List<room>();
    room LTBROpenedRoom = new room(true, true, true, true, LTBROpenedRoomObject);
    rooms.Add(LTBROpenedRoom);
    room ROpendRoom = new room(false, false, true, false, ROpenedRoomObject);
    rooms.Add(ROpendRoom);
    room LOpendRoom = new room(false, false, false, true, LOpenedRoomObject);
    rooms.Add(LOpendRoom);
    room TOpendRoom = new room(true, false, false, false, TOpenedRoomObject);
    rooms.Add(TOpendRoom);
    room BOpendRoom = new room(false, true, false, false, BOpenedRoomObject);
    rooms.Add(BOpendRoom);

    room LROpendRoom = new room(false, false, true, true, LROpenedRoomObject);
    rooms.Add(LROpendRoom);
    room TBOpendRoom = new room(true, true, false, false, TBOpenedRoomObject);
    rooms.Add(TBOpendRoom);

    room LBOpendRoom = new room(false, true, false, true, LBOpenedRoomObject);
    rooms.Add(LBOpendRoom);
    room RBOpendRoom = new room(false, true, true, false, RBOpenedRoomObject);
    rooms.Add(RBOpendRoom);
    room RTOpendRoom = new room(true, false, true, false, RTOpenedRoomObject);
    rooms.Add(RTOpendRoom);
    room LTOpendRoom = new room(true, false, false, true, LTOpenedRoomObject);
    rooms.Add(LTOpendRoom);

    room LBROpendRoom = new room(false, true, true, true, LBROpenedRoomObject);
    rooms.Add(LBROpendRoom);
    room BRTOpendRoom = new room(true, true, true, false, BRTOpenedRoomObject);
    rooms.Add(BRTOpendRoom);
    room LTROpendRoom = new room(true, false, true, true, LTROpenedRoomObject);
    rooms.Add(LTROpendRoom);
    room LTBOpendRoom = new room(true, true, false, true, LTBOpenedRoomObject);
    rooms.Add(LTBOpendRoom);

    List<room> possibleRooms = new List<room>();
    List<room> roomsToRemove = new List<room>();
    possibleRooms = rooms;
    roomsToRemove = rooms;

    room[] placedRooms = new room[arrayLenght];

    Vector3[] walkerPositions = new Vector3[arrayLenght];

    placedRooms[0] = starterRoom;

    placedRooms[0].Spawn(transform.position);

    walker = transform.position;

    walkerPositions[0] = walker; //Startraum

    while(walkCounter < arrayLenght) {
        Walk();
    }
    void Walk() {
        int rnd = Random.Range(1, 4);
        

        switch (rnd) {
            case 1: walker.y += 30; break; //Walker geht 1 Raum nach oben
            case 2: walker.x += 30; break; //Walker geht 1 Raum nach rechts
            case 3: walker.y -= 30; break; //Walker geht 1 Raum nach unten
            case 4: walker.x -= 30; break; //Walker geht 1 Raum nach links
        }
        foreach(Vector3 pos  in walkerPositions) { //Prüft ob Walker schon an seinem jetzigen Ort war
            if(walker == pos) {
                alreadySet = true;                 //Falls ja wird dieser Bool auf true gesetzt
                break;
            }
        }
        if(!alreadySet) { //Wenn er noch nicht da war
            walkerPositions[walkCounter] = walker;
            walkCounter++;

            if(Physics2D.OverlapCircle(new Vector2(walker.x - 20, walker.y), 3f)) {
                if(Physics2D.OverlapCircle(new Vector2(walker.x - 20, walker.y), 3f).name != "Right" && !Physics2D.OverlapCircle(new Vector2(walker.x - 20, walker.y), 5f).CompareTag("walls")) {
                    checkLeft = true;
                }
            }
            if(Physics2D.OverlapCircle(new Vector2(walker.x + 20, walker.y), 3f)) {
                if(Physics2D.OverlapCircle(new Vector2(walker.x + 20, walker.y), 3f).name == "Left" && !Physics2D.OverlapCircle(new Vector2(walker.x + 20, walker.y), 3f).CompareTag("walls")) {
                    checkRight = true;
                }
            }
            if(Physics2D.OverlapCircle(new Vector2(walker.x, walker.y + 20), 3f)) {
                if(Physics2D.OverlapCircle(new Vector2(walker.x, walker.y + 20), 3f).name == "Bottom" && !Physics2D.OverlapCircle(new Vector2(walker.x, walker.y + 20), 3f).CompareTag("walls")) {
                    checkTop = true;
                }
            }
            if(Physics2D.OverlapCircle(new Vector2(walker.x, walker.y - 20), 3f)) {
                if(Physics2D.OverlapCircle(new Vector2(walker.x, walker.y - 20), 3f).name == "Top" && !Physics2D.OverlapCircle(new Vector2(walker.x, walker.y - 20), 3f).CompareTag("walls")) {
                    checkBottom = true;
                }
            }

            if(checkLeft) {
                foreach(room room in roomsToRemove) {
                    if(!room.leftOpened) {
                        roomsToRemove.Remove(room);
                    }
                }
            }
            if(checkRight) {
                foreach(room room in possibleRooms) {
                    if(!room.rightOpened) {
                        roomsToRemove.Remove(room);
                    }
                }
            }
            if(checkTop) {
                foreach(room room in roomsToRemove) {
                    if(!room.topOpened) {
                        roomsToRemove.Remove(room);
                    }
                }
            }
            if(checkBottom) {
                foreach(room room in roomsToRemove) {
                    if(!room.bottomOpened) {
                        roomsToRemove.Remove(room);
                    }
                }
            }

            foreach(room room in roomsToRemove) {
                possibleRooms.Remove(room);
            }
            possibleRooms[Random.Range(0, possibleRooms.Count)].Spawn(walker);
            possibleRooms = rooms;
            roomsToRemove = rooms;
            checkLeft = false;
            checkRight = false;
            checkTop = false;
            checkBottom = false;
        } else {
            switch (rnd) { //Wenn er schon da war dann läuft er zurück
                case 1: walker.y -= 30; break; 
                case 2: walker.x -= 30; break; 
                case 3: walker.y += 30; break; 
                case 4: walker.x += 30; break; 
            }
            alreadySet = false;
        }
    }
}

}

You cannot modify a collection while you iterate over it with a foreach loop.

I have a doc that presents alternatives based on what you’re doing. Alternatively you can also use RemoveAll:

Example

if (checkRight)
    possibleRooms.RemoveAll(room => !room.rightOpened);