44

I just made a silly mistake and I ended up with an infinite loop inside my Update(). After that I wasn´t able to stop the Play Mode. Actually I wasn´t able to do anything else with Unity until I restarted it.

My question is, does anyone know how to stop the Play Mode in Unity gracefully? Is there any shortcut or some lines of code to force a timeout?

Extra Note: I have been looking for a solution to this issue for a while, unfortunately without success. However I found a solution to a side-effect of this problem. When you press ctrl-alt-del you can loose everything you haven´t save in your scene (which can be hours of work).

So Unity does an auto-save when you hit play, and the scene backup is in the Temp folder, as long as you haven´t run Unity again after a force-quit.

4
  • 4
    You can't from Unity itself. You have to kill Unity from Task Manger when that happens.. Post the code that's causing that problem and you will likely get an answer.
    – Programmer
    Commented Aug 31, 2017 at 10:15
  • Thanks for the answer. Actually I found the problem myself, as I said in the question I wrote a while() instead of if() in something that for testing I left true all the time. Commented Aug 31, 2017 at 10:18
  • 1
    Never used it myself, but there is an (paid) asset on the store to stop Unity in this kind of situation.
    – Hellium
    Commented Aug 31, 2017 at 13:28
  • Old question, but: if you're in the editor, make a change to one of your MonoBehavior scripts - say, add syntax error - and switch back to the editor. In most cases, it'll attempt to recompile the script, which generates an error, and will usually exit Play mode.
    – 3Dave
    Commented Aug 29, 2022 at 15:48

6 Answers 6

30

The following worked for me (Props to @LinusR and by extension @Kinxil) This is just a quick step-by-step.

This should work for you if you are using Visual Studio w/ Unity Tools.

Find the loop:

  1. Open Visual Studio (if not already open)
  2. Click Attach to Unity (if not already attached)
  3. Click Break All (pause II symbol)
  4. Open the Call Stack, Threads and Immediate windows. (All in Debug → Windows →)
  5. Looking at the Call Stack, click through the threads in the Threads window.
  6. Stop when you find the thread that the loop is on. (Call Stack helps with this)
  7. You must be on the thread with the loop to execute necessary commands in the Immediate window.

Now get me out of here!:

[LinusR's solution seemed to be the most bullet-proof and versatile.]

Break the loop with a null value and some other options

  1. In the Immediate window, set one of the nullable objects/fields/properties used in the loop to null e.g. for Thread.SpinWait.SpinUntil(() => someObject.NeverTrue());
    • someObject = null;
    • Unity will respond again in this instance (providing someObject remains null).
  2. An alternative for SOME loops is simply breaking and changing the instruction or values and/or dragging the current instruction arrow (yellow arrow) out of the loop, though this may not be possible depending on the type of loop.
    • Think about how the loop works; How often is it executed? Will it be called each frame? etc.
  3. Nothing working? Read the other answers here, Get creative with the Immediate window. Also in future it would be wise to have Error Pause enabled at all times in Unity.
0
19

I just got into this situation: accidental infinite loop, stuck in play mode on a scene with unsaved work, Unity unresponsive.

But I was lucky enough to have Monodevelop open, even though I was mainly using Sublime for scripting. I pressed the button to start debugging near the top left of the Monodevelop window, then hit the pause button. Execution paused on a line in the middle of my infinite loop. Windows task manager confirmed Unity was no longer locking the CPU.

In Monodevelop, I was then able to find an object obj that the next line would attempt a method call on, and use the "Immediate" window to execute obj = null. Then unpause. Unity itself now unlocks because of the null pointer error, and I could take it out of play mode and save my work.

(Unity 2017.4.1f1 Personal, Windows 10 Home x64, Monodevelop 5.9.6)

Note, I got the idea from reading @Kinxil's answer, but I had to take a slightly different approach because there was no "blocking value". I had a for (;;) loop that had previously been inside a coroutine and I changed it to be inside a FixedUpdate() without removing the loop. :\ So causing an exception was the only option I could think of.

17

Solution

  1. Attach your IDE to Unity
  2. Pause execution
  3. Exit the loop
  4. Optionally execute Debug.Break() to prevent the loop to enter again at the next Update()
  5. Resume execution
  6. Return to the Unity editor

For Visual Studio

(I haven't tested the other IDE's)

  1. Click Attach to Unity
  2. Wait for the process to attach (sometimes take a while)
  3. Click Break All (pause II symbol)
  4. Click on menu Debug → Windows → Threads
  5. Click on the thread that contains your source code (see location column)
  6. In your source code (text editor), move the program pointer (yellow arrow) outside the loop by dragging it to a different line in your source code.

Optionally in the case where the loop is inside some Update() event that would re-enter the loop again:

  1. Click on menu Debug → Windows → Immediate
  2. Type UnityEngine.Debug.Break() inside the Immediate Window and press Enter

Finally resume execution (Play button) and return to Unity editor. Unity should now be in a "Paused" state where you can inspect what went wrong or "Stop" the Play Mode.

12

For me, this worked.

How To Get out of A infinite loop

  1. Open Visual Studio.
  2. Click "Attach to Unity" at the top (if not already clicked)
  3. Add a breakpoint to the infinite loop by clicking the area to the left of the line number where the loop is.
  4. You will see a yellow arrow where the breakpoint is. Click and drag this arrow to a spot outside the loop (preferably after)
  5. Remove your current breakpoint.
  6. Go back to unity and it will most likely take a second to unfreeze but after it unfreezes, you should be able to click the play button to stop play mode.
  7. Boom! Panic attack averted.
0
2

It's a shame, Unreal Engine is for the most part really good at detecting infinite loops and exiting when they do, but Unity almost never seems to. It's tough to find out what's going on because Debug.Log doesn't fire until the frame completes so you can't see what's going on. A workaround to just try and see what's going on is to use some sort of limiter like this:

int limiter = 0, loopCap = 10000;

while (SomeCondition && limiter < loopCap)
{
    // Do something
    limiter++
}
if (limiter >= loopCap) Debug.Log("Infinite Loop avoided");

Of course, that's a much easier example because while loops can very easily become infinite loops, but if you have a ball park for where it could be happening, you could put a limiter there.

And this is only for debugging purposes so you're not having to cancel it via Task Manager. Ideally, you can find out what was the problem and remove the limiter.

I had an infinite loop a few days ago and I had no idea where it was going wrong, so I just commented out the code and then uncommented it out until it broke again, and was able to figure out where it came from.

0

Easily said (for MS Visual Studio):

  1. Attach the debugger.
  2. Add a breakpoint at a line that is called by the the infinite loop.
  3. When the break point is reached, modify a variable to cause e.g. an null exception in consequence. You can do that in the "Locals" window (should be open when debugging; otherwise Debug/Windows/Locals or Alt+4).
  4. Continue execution in the code editor.

Result: The Unity IDE indicates the exception and you can hit the stop-button.

Nothing else was needed in my case. Alternatively, in step 3 you might be able to change a local variable to a value that just ends the loop without an exception. But that will be only then useful, if that inifinite loop code is not called directly again afterwards.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.