Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

unity3d

I just made a silly mistake and inside the void Update() instead of writting an if(var) I wrote while(var) and the var was never modified inside the while, so I ended up programming an infinite loop inside my Update()

The story itself is not really important. The important thing is I wasn´t able to stop the Play Mode. Actually I wasn´t able to do anything else with Unity until I restarted.

So 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?

I know the best way is to avoid infinite loops, but somethings you can make a silly typo.

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.

like image 896
Ignacio Alorre Avatar asked Aug 31 '17 10:08

Ignacio Alorre


People also ask

How do you exit play mode in Unity?

Click the play button to exit Play mode.


1 Answers

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.
like image 138
aaronedmistone Avatar answered Sep 23 '22 00:09

aaronedmistone