Search Unity

how to kill running game while at a breakpoint?

Discussion in 'Editor & General Support' started by steveh2112, Feb 14, 2020.

  1. steveh2112

    steveh2112

    Joined:
    Aug 30, 2015
    Posts:
    314
    i know i have an infinite loop and i caught it at a breakpoint, but when stopped at a breakpoint, it looks like unity is frozen. is there no way to kill the game process so i don't have to kill unity and loose all my scene edits?
     
  2. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    4,196
    It usually depends on the specific code that caused the infinite loop. Let's say it was something like `while(someVariable)', you could put a breakpoint there and change the value of 'someVariable' and that might break out of the loop. Another option, if you're at a breakpoint, is you can drag the little yellow arrow near the line number market, and have Visual Studio resume execution of the code at a later point, possibly skipping out of the loop itself.

    That usually only works for one-off method calls, though. If your infinitely-looping code is being called by Update, for example, then it's probably going to get called again before you have any chance to get back to Unity and stop it.

    It's for reasons like this that I highly recommend downloading some editor code to automatically save your scene whenever you enter play mode, so that you won't lose any content if this sort of thing happens.
     
  3. ArachnidAnimal

    ArachnidAnimal

    Joined:
    Mar 3, 2015
    Posts:
    1,835
    You can try to use
    Code (csharp):
    1.  
    2. #if UNITY_EDITOR
    3.         UnityEditor.EditorApplication.isPlaying = false;          
    4. #endif
    5.  
    to force Unity to exit playmode.
     
  4. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    4,196
    If the code is in an infinite loop, say a 'while' loop, how would you get it to jump to the code you posted? That code is fine for exiting Play mode in general, but I don't know how you'd trigger it if you're already stuck in a loop.
     
  5. ArachnidAnimal

    ArachnidAnimal

    Joined:
    Mar 3, 2015
    Posts:
    1,835
    It's a bit hacky but here is one way:
    Add a Windows registry key, call it say "QuitPlayMode". Then read the value in the registry in the loop. If it was set to non-zero, quit the playmode.

    Code (csharp):
    1.  
    2.     while (true) {
    3.  
    4.             #if UNITY_EDITOR
    5.             var forceQuit = PlayerPrefs.GetInt ("QuitPlayMode"); //Read the value in the registry
    6.             if (forceQuit > 0) {      
    7.                 UnityEditor.EditorApplication.isPlaying = false;
    8.                 return; //This is required for some reason
    9.             }
    10.             #endif
    11.  
    12.         }
    13.  
    If Unity is hung in the loop and you want to exit playmode, go to the Registry Editor, and manually change the key "QuitPlayMode" to a non-zero value, the value will be read in the loop and force Unity to exit playmode.
    I tried it here and it works.
    Of course it would require you to manually enter the code in the loop if you were testing and expected it to get hung in the loop
    I can't think of any other way right now to execute that code

    reg.png
     
    Last edited: Feb 19, 2020
  6. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    4,196
    Yeah, that's pretty much why this is impractical. Every infinite loop I'm dimly created was done completely by accident, where I forgot to include one line of code, or some other dumb mistake. I'd love to see if there was some trick to getting the debugger to resume at some completely different line of code without having to prepare that in the code first.
     
  7. steveh2112

    steveh2112

    Joined:
    Aug 30, 2015
    Posts:
    314
    2016 and unity still hasn't fixed this!
     
  8. bobisgod234

    bobisgod234

    Joined:
    Nov 15, 2016
    Posts:
    1,042
    Did none of the replies work for you?
     
  9. steveh2112

    steveh2112

    Joined:
    Aug 30, 2015
    Posts:
    314
    it doesn't work at add that line at run time i guess. i suppose if you know in advance that there will be an infinite loop there you can add a line, but if you know that , why let it get in there in the first place.