How to pause the Unity editor while at a debug breakpoint

I’ve connected the Visual Studio, set a breakpoint and successfully stopped at that point. Now I want to pause Unity and take a look at the current state of the scene from inside the Unity editor. But debugging in VS seems to lock the Unity editor up completely and I can’t even switch to it let alone CTRL+SHIFT+P. If I hit Continue first, the scene will change and it will be too late.

Is there some way to pause the Unity editor from C# code? Any other suggestions?

1 Like

I think Debug.Break() is how you’re supposed to pause the Unity editor from within code, but it seems to wait until the end of the frame to pause.

Eg if I do: Debug.Log("hi"); Debug.Break(); Debug.Log("bye");, the editor will pause but I see “bye” in the console (and a lot of other stuff which destroys the state I’m trying to analyze). This might be as good as it gets?

More tip I discovered for Debug.Break();

If you want to pause an editor from debugger, the frame you are debugging right there right now.
_

Practical Example:

For instance, while in debugger, if you want to see object in scene after Instantiate was called, but the play mode is still running, it would be too late to hit F5 (debugger continue), switch back to editor, pause the run, and see the object. As many frames would have elapsed.
_
Solution:

= Add new watch expression and paste UnityEngine.Debug.Break() into a line. This will execute a code like it is running from your script. Which will sent to editor.

= Note that Value is displaying “Unable to evaluate” because it is function with no return value so nothing for watch window to show, this is normal.


_
= Continue your code as usual (F5). Now switch back to editor and notice that editor is paused.

= That watch line will not repeatly executed because expression is function. If you want it to run again, you could click at reevaluation button (round circle arrow)