Code freezes PC, am I doing something wrong here?

Hi guys,

After about 5 mins of this code running the PC freezes up and I have to reboot. I also have this same code for spawning walls. Am I doing something wrong here?

Thanks.

//-----------------------------------------------------Spawn SandBags-----------------------------------------------//

    void SpawnSandbag(){

        if(ESandbagAmountSpawned >= ESandbagMax){
            canSpawnSandbag = false;
            return;
        }
        if(GameController.cashEnemy < 30){
            canSpawnSandbag = false;
            return;
        }

        if(canSpawnSandbag){

            print("Start Spawning Sandbag");

            if(ESandbagAmountSpawned < ESandbagMax){

                if(GameController.cashEnemy > 30){

                    if(CommandAmountSpawned > 0){
                       
                        for (int i = 0;i<SandbagSP.Count;i++) {

                            if(SandbagIndex == ESandbagMax){ // Reset the index based on the List count
                                SandbagIndex = 0;
                            }

                            if(!SandbagSP[i].isTaken) {

                                SandbagSP[i].isTaken = true;

                                Transform SpawnPos = SandbagSP[SandbagIndex].transform;

                                Transform newSB = PoolManager.Pools["Turrets"].Spawn(Sandbag, SpawnPos.transform.position, SpawnPos.transform.rotation);

                                newSB.GetComponentInChildren<SandbagHealth>().SBSP = SandbagSP[i];

                                GameController.cashEnemy -= 30;

                                if(GameController.cashEnemy < 0){
                                    GameController.cashEnemy = 0;
                                }

                                SandbagIndex ++;
                                ESandbagAmountSpawned ++;

                                print("Spawned a Sandbag");

                                canSpawnSandbag = false;
                                break; // break out here so it only spawns 1 sandbag from the index.
                            }

                            if(ESandbagAmountSpawned >= ESandbagMax){
                                canSpawnSandbag = false;
                                break;
                            }
                            if(GameController.cashEnemy < 30){
                                canSpawnSandbag = false;
                                break;
                            }


                        }
                    }else{
                        canSpawnSandbag = false;
                        SpawnCommand();
                        return;
                    }
                }else{
                    canSpawnSandbag = false;
                    return;
                }
            }else{
                canSpawnSandbag = false;
                return;
            }
        }
    }

just wondering, the SandbagSP List has 56 transforms, and every 10 seconds it is iterating through them and spawning at the 1st location that is not taken.

Could this be causing the Freeze?

From my experience, Unity freezes when you have infinite loop (I don’t see that here, but I don’t know how exactly PoolManager.Pools[ ].Spawn works) and OS freezes when you have hardware problems/driver problems/extremely heavy disc activity (one of reasons is lack of RAM, and, thus, usage of HDD for swapping - can usually be heard and PCs often has lamp for HDD activity though).

Thanks for looking, I thought my code was ok.

Yeah every where I have been reading freeze was due to memory. It is weird cause I monitor the pc memory activity and it always sits at 2.6g and I have 4g ram, 8 core amd. The profiler sits the cpu at 3.0ms and gpu at 0.6ms and Batches are at 100. No memory spikes or leaks, just a freeze and it does spawn fine for a while.

Yeah may do some more testing with the PoolManager, but I dont think that is the case because it also spawns my buildings and that never crashes the pc.

When I do not use the code to spawn walls or sandbags pc runs fine, so I am assuming it is my code.

Using 5.2.3 btw.

Try pulling everything out of the scene and reducing it down to just the offending code.

Also several of those if statements seem unnecessary. For example if “ESandbagAmountSpawned < ESandbagMax” weren’t true, you would have broken out of the function up in the first couple lines anyhow. Why test that again?

But yeah, a freezing game usually means an infinite loop or something, freezing system usually is more system specific (memory/disk/etc), and nothing in this appears to touch on that. But we don’t have any context outside of this function alone, are you sure this code is the offending code?

Yeah I have actually tried to just disable that part from running with a bool check, and if I disable them, game runs fine.

I really did that double if check there to be sure it was doing its job there… the crashing was making me over cautious I guess…

Here is a shot from the profiler.

I will let it run again with spawning sandbags and walls disabled and only spawning buildings for 10min or so, if no crash, then I will try to spawn just the sandbags and see if it freezes again.

Just ran the game for an hour only spawning buildings, everything went fine.

I have got rid of those double if checks now :wink:

Now I will only enable sandbags to spawn.

Ok found the problem!

All it was, was a missing script on the sandbag prefab ( I never dragged and dropped it there) that would update the grid when it was placed down, so the grid could not get updated and that Froze my PC.

Pretty weird how 1 missing script could do that!