Weird infinite loop-or just long loading time

Hello, this going to be a bit of a hefty ask. I’m working on topdown terrain generation, and because I’m stubborn, I wanted to make my own system without using colliders. I’ve debugged the 141-line long script down to 1 buggy foreach loop. It seems to be running an infinite loop, but it could just take a long time to run. Here’s the loop:

foreach(GameObject spawnpoint in spawnpoints)
        {
            if(spawnpoint != null)
            {
                GameObject tempRoom = Instantiate(room, spawnpoint.transform.position, spawnpoint.transform.rotation);
                tempRoom.GetComponent<OutdoorRoom>().chains = chains + 1;
                tempRoom.transform.parent = spawnpoint.transform.parent.transform.parent;
                
                if(System.Array.IndexOf(spawnpoints, spawnpoint) == 0)
                {
                    Destroy(tempRoom.GetComponent<OutdoorRoom>().spawnpoints[1]);
                    tempRoom.GetComponent<OutdoorRoom>().exits += "S";
                }
                if (System.Array.IndexOf(spawnpoints, spawnpoint) == 1)
                {
                    Destroy(tempRoom.GetComponent<OutdoorRoom>().spawnpoints[0]);
                    tempRoom.GetComponent<OutdoorRoom>().exits += "N";
                }
                if (System.Array.IndexOf(spawnpoints, spawnpoint) == 2)
                {
                    Destroy(tempRoom.GetComponent<OutdoorRoom>().spawnpoints[3]);
                    tempRoom.GetComponent<OutdoorRoom>().exits += "W";
                }
                if (System.Array.IndexOf(spawnpoints, spawnpoint) == 3)
                {
                    Destroy(tempRoom.GetComponent<OutdoorRoom>().spawnpoints[2]);
                    tempRoom.GetComponent<OutdoorRoom>().exits += "E";
                }
            }
        }

If I need to provide more code, I’m happy to. I’ll be constantly working to fix the bug in the meantime.
Thanks for everything,
EpicCrownKing

Oh and Task Manager doesn’t deem Unity as not responding, it just takes up about 80% of my PC.
CORRECTION /\ Yes, it does say that it isn’t responding. Whoops!

I don’t know if the following will fix your issue, but it will improve the performances a lot

for(int index = 0 ; index < spawnpoints.Length ; ++index)
{
    GameObject spawnpoint = spawnpoints[index];

    if(spawnpoint == null) continue;

    OutdoorRoom tempRoom = Instantiate(room, spawnpoint.transform.position, spawnpoint.transform.rotation).GetComponent<OutdoorRoom>();
    tempRoom.transform.parent = spawnpoint.transform.parent.transform.parent;
    
    switch(index)
    {
        case 0:
            Destroy(tempRoom.spawnpoints[1]);
            tempRoom.exits += "S";
            break;
        case 1:
            Destroy(tempRoom.spawnpoints[0]);
            tempRoom.exits += "N";
            break;
        case 2:
            Destroy(tempRoom.spawnpoints[3]);
            tempRoom.exits += "W";
            break;
        case 3:
            Destroy(tempRoom.spawnpoints[2]);
            tempRoom.exits += "E";
            break;
    }
}

I was being dumb and looked at the wrong piece of code. Thanks for the help @Hellium. The optimized code sped it up even more.