- Home /
How to stop editor from running a dead loop.
I made some logic error which caused the game to run into an infinite loop and the editor freezes and don't take any of my input. Is there a way to stop it or insert a break point during the dead loop process? Now I can only use task manager to kill it.
Answer by Veehmot · Jun 17, 2013 at 10:07 PM
There isn't, but you can force a break in the code using Debug.Break.
This is useful when you want to check certain values on the inspector and you are not able to pause it manually.
A nice find, but you would still need to somehow know when a loop is stuck before calling Debug.Break(). $$anonymous$$aybe by using a timer?
You know how it's done usually in other environments, by using a counter inside a while loop, and if that counter is greater than an arbitrary number, you stop the execution.
Answer by Jamora · Jun 17, 2013 at 08:10 PM
There is no way to resume an unresponsive Unity editor that I know of. But as a way to prevent your editor from freezing in the first place, always place all while loops (and others that could potentially get stuck) in a coroutine.
Answer by bubzy · Jun 17, 2013 at 03:07 PM
take the loop out of your code.
check to see if theres some way you can use an "if" statement within your Update() function.
I mean to stop it at runtime. If I know there is a dead loop before I run I will surely get rid of it.
Answer by GilesDMiddleton · Oct 05, 2015 at 05:52 PM
Infinite loops can easily happen in untested or complex loops.
Typically you have to kill the process to get out of them. This isn't so much of a problem unless you have unsaved changes. You can, however save your unsaved changes by calling methods on the EditorApplication to save the scene. See this post http://answers.unity3d.com/answers/1076468/view.html I wrote yesterday.
One way to protect yourself against infinite loops, and occasional stupidity, could be to use a static bool variable, lets call it
static bool bPanicButton=false;
while( true && !bPanicButton )
{
<do things>
}
Debug-break the loop by setting a breakpoint in the debugger and then change bPanicButton
to true
in the debugger- you can change the value of variables in the Locals/watch window.
I have also used
Destroy(this);
To get out of some situations, but rarely works.
Answer by meat5000 · Oct 05, 2015 at 05:26 PM
On a similar thread, the suggestion was to open Mono and attach the debugger. You can then use Mono to Pause the running game in Unity. Win!