Unity – How to stop Play Mode in case of infinite loop?

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.

Leave a Comment