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!
Answer by Hellium · Jan 20, 2020 at 07:36 PM
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;
}
}
Never considered a switch statement. Whoops! That'll be really helpful. I'll plug that in and see if it works. It's almost like you know my code better than I do(which probably is the case)! Thanks a lot!
It still lags out, I think there's an infinite loop. I'll work within my old code, make it work, then translate it into your optimized code. I'll certainly credit you in my code.
Consider using the profiler to know where does the problem come from:
Answer by EpicCrownKing · Jan 20, 2020 at 10:26 PM
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.