Can the Unity editor break out of an infinite loop in a script?

Hi, when I put an infinite loop in a script (on purpose, of course:) ), the whole Unity editor hangs when I press 'play'. Is there a way to break out of this? It seems a bit harsh to have to end the editor process and imho also contrary to the idea of scripting?

I know it’s an old question but I do have a better solution.

As others have said: don’t use an infinite loop by design. However if you did it by mistake and don’t want to loose a whole scene or other unsaved data here’s what you can do:


  1. Open mono develop and attach it to the editor if you haven’t already.(Run > Attach To Process)

  2. Pause execution from mono develop.(Run > Pause)

  3. Go to your immediate window (View > Debug Windows, if you don’t see it at the bottom).

  4. Modify the value that’s causing you to loop infinitely. The Immediate window lets you just type in some code that will execute in that scope.


For me, the problem was looping over a list and trying to find a specific value that matched an element. I didn’t properly handle the value not existing so it looped infinitely. All I had to do was call listname.Clear(); in the immediate window and the loop broke out on the next iteration.

If you were doing something exceptionally silly like while(true){ /* some silly code */ } you might be able to break out by just calling return. If you were calling that function every frame you might have to be a bit more sneaky and call T.Destroy(gameObject.GetComponent()) or something to that effect to remove the script that’s calling that silly function.

I hope this helps someone else who doesn’t want to nuke their work in progress!

Cheers.

Short answer - unfortunately, no - if you have already got Unity frozen in an infinite loop, your only option is to force-quit.

However, this question makes me wonder why you're putting an infinite loop in there on purpose in the first place. What are you using it for?

In general, in Unity, you would use the Update() and FixedUpdate() functions for "game loop" type code, because these functions get called every frame, and every physics step respectively, whilst releasing control to Unity's other systems (rendering, physics, etc) between steps so as to not lock up the entire thread. Eg, to some action every frame:

function Update() {
    // increment a variable 'i' by 1 each frame:
    i++;
}

Another method of creating a non-hanging loop is to use a Coroutine, which is a type of function which allows you to explicitly specify certain points where the execution should be paused and control released to other processes until a certain time.

An example of a "non hanging" loop in a coroutine, in Unity's Javascript:

function MyLoop() {
    while (true) {
        print("hello world!);
        yield;
    }
}

And in C#:

IEnumerator MyLoop () {
    while (true) {
        Debug.Log("hello world!);
        yield return null;
    }
}

Hope this is helpful.

Apologies for the slightly off-topic response.
However, when people get into an infinite loop and think they’ve lost their scene and come looking for help on this thread, this will be most useful to them:

without starting unity again. This is important, DO NOT start unity again
Go into your game folder (in myDocuments usually I guess)
Open the Temp folder in the game folder and look for a file called
" __EditModeScene "
Rename this file to the name of your scene in unity that was running when it crashed, so myScene.unity or whatever. Take this file and paste it into the Assets folder in your game folder, overwriting(or preferably backing up first) your scene of the same name.
Open unity again and your scene will be exactly as it was when you pressed Play and crashed the program.

You’re welcome.

Hi @MvD !

Yes, it’s possible with a plugin that I created which breaks infinite loops through a single shortcut.

With it, just press Shift+Esc and the editor is responsive again in a flash.

You can watch it in action here: - YouTube

I’m offering it on the Asset Store, it’s called Panic Button :slight_smile: Panic Button | Utilities Tools | Unity Asset Store

I think it would be better to avoid an infinite loop as it is likely to make most IDE's hang (at least temporarily).

Yea, but in this case the unity editor shouldn’t freeze up and force you to force quit the application. You ought to be able to hit a button in unity to stop the script. I guess for some reason unity offers up an important thread to the user code (which way be responsible for rendering the UI as well, which would explain the hanging.

I feel like this could be fixed in Unity. Though maybe they are dealing with some edge cases since you can write scripts which add components to the UI. But you could imagine an MVC setup that would work better and still render the UI on a different thread than user code. It seems very strange to me that it does this. Its not like you have to force quit eclipse if you write an inf loop in your java code.

I know this is an old question, but this is the answer:

-Open mono
-Attach, set breakpoint where the infinite loop is occuring
-Open Local variables
-Alter the value of the variable causing the loop
-Profit

We’ve also already had this as an old answer :wink: Yes, this will work, as long as you know where in the code the infinite loop is happening, which, if you pause debugging in Monodevelop and check out the different threads and stacks of those threads, you should be able to find. Fingers crossed.

BTW i just did this:

for (int x = 0; x < columns + 1 ; x++) {    // loop x
	 for (int y = 0; x < rows + 1 ; y++) {  // loop y
                     ^
                     |

And my ram got to 100% and i got a blue screen of dead. (windows 8.1 - unity 5.1.0f3)

So that is your answer. Far to late but it is finaly there.

Unity can not break out some infinite loops.

There is a way to save your scene before killing the app.
If you can get a breakpoint in your infinite loop, by attaching the debugger, type this in the immediate window…

EditorApplication.SaveScene("Assets/MyNewScene.unity");

Then press play again. Hit your breakpoint again.
Type it again to make sure, then look in your assets folder.

Then to quit the editor, type

EditorApplication.Exit(0);

I tried this with a FixedUpdate() containing

while (true) {
			int x=0;
			x=x+1;
			Debug.Log ( x.ToString() );
		}

Hey, hey, hey! I broke out of it-i think. I was soo pissed off when I pressed play and Unity just stopped working. I froze and my whole computer was going slow for about 10 minutes. Then, I tried to press the pause button on the unity editor and about five minutes later unity paused-few!I would of lost all my work if it hadn’t

With all due respect, you should not even be trying to do such a thing with the engine, but with the code. I know it's really annoying when somebody won't answer your question. But I'm just trying to share my experience on this, and the answer I can give you is , don't get an infinite loop, EVER!

The reason is simply because it doesn't get out, and why should anything else then the code be responsible? It shouldn't, the code is. Otherwise it's going to get really confusing when the responsibility starts shifting to places where they don't belong.

You should write a script to check your code for infinite loops first just in case…