Search Unity

  1. If you have experience with import & exporting custom (.unitypackage) packages, please help complete a survey (open until May 15, 2024).
    Dismiss Notice
  2. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice

Code freezes PC, am I doing something wrong here?

Discussion in 'Scripting' started by Jaysta, Feb 12, 2016.

  1. Jaysta

    Jaysta

    Joined:
    Mar 13, 2013
    Posts:
    141
    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.

    Code (CSharp):
    1. //-----------------------------------------------------Spawn SandBags-----------------------------------------------//
    2.  
    3.     void SpawnSandbag(){
    4.  
    5.         if(ESandbagAmountSpawned >= ESandbagMax){
    6.             canSpawnSandbag = false;
    7.             return;
    8.         }
    9.         if(GameController.cashEnemy < 30){
    10.             canSpawnSandbag = false;
    11.             return;
    12.         }
    13.  
    14.         if(canSpawnSandbag){
    15.  
    16.             print("Start Spawning Sandbag");
    17.  
    18.             if(ESandbagAmountSpawned < ESandbagMax){
    19.  
    20.                 if(GameController.cashEnemy > 30){
    21.  
    22.                     if(CommandAmountSpawned > 0){
    23.                        
    24.                         for (int i = 0;i<SandbagSP.Count;i++) {
    25.  
    26.                             if(SandbagIndex == ESandbagMax){ // Reset the index based on the List count
    27.                                 SandbagIndex = 0;
    28.                             }
    29.  
    30.                             if(!SandbagSP[i].isTaken) {
    31.  
    32.                                 SandbagSP[i].isTaken = true;
    33.  
    34.                                 Transform SpawnPos = SandbagSP[SandbagIndex].transform;
    35.  
    36.                                 Transform newSB = PoolManager.Pools["Turrets"].Spawn(Sandbag, SpawnPos.transform.position, SpawnPos.transform.rotation);
    37.  
    38.                                 newSB.GetComponentInChildren<SandbagHealth>().SBSP = SandbagSP[i];
    39.  
    40.                                 GameController.cashEnemy -= 30;
    41.  
    42.                                 if(GameController.cashEnemy < 0){
    43.                                     GameController.cashEnemy = 0;
    44.                                 }
    45.  
    46.                                 SandbagIndex ++;
    47.                                 ESandbagAmountSpawned ++;
    48.  
    49.                                 print("Spawned a Sandbag");
    50.  
    51.                                 canSpawnSandbag = false;
    52.                                 break; // break out here so it only spawns 1 sandbag from the index.
    53.                             }
    54.  
    55.                             if(ESandbagAmountSpawned >= ESandbagMax){
    56.                                 canSpawnSandbag = false;
    57.                                 break;
    58.                             }
    59.                             if(GameController.cashEnemy < 30){
    60.                                 canSpawnSandbag = false;
    61.                                 break;
    62.                             }
    63.  
    64.  
    65.                         }
    66.                     }else{
    67.                         canSpawnSandbag = false;
    68.                         SpawnCommand();
    69.                         return;
    70.                     }
    71.                 }else{
    72.                     canSpawnSandbag = false;
    73.                     return;
    74.                 }
    75.             }else{
    76.                 canSpawnSandbag = false;
    77.                 return;
    78.             }
    79.         }
    80.     }
     
    Last edited: Feb 12, 2016
  2. Jaysta

    Jaysta

    Joined:
    Mar 13, 2013
    Posts:
    141
    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?
     
  3. Teravisor

    Teravisor

    Joined:
    Dec 29, 2014
    Posts:
    654
    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).
     
  4. Jaysta

    Jaysta

    Joined:
    Mar 13, 2013
    Posts:
    141
    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.
     
  5. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,555
    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?
     
  6. Jaysta

    Jaysta

    Joined:
    Mar 13, 2013
    Posts:
    141
    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.
     
  7. Jaysta

    Jaysta

    Joined:
    Mar 13, 2013
    Posts:
    141
    Just ran the game for an hour only spawning buildings, everything went fine.

    I have got rid of those double if checks now ;)

    Now I will only enable sandbags to spawn.
     
  8. Jaysta

    Jaysta

    Joined:
    Mar 13, 2013
    Posts:
    141
    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!