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?
Answer by sarahnorthway · Mar 07, 2016 at 12:41 AM
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?
Note to future reader. Debug.Break will only signal Unity editor to pause after current frame update has completed. That's mean all script has run for 1 completed cycle. So your "bye" will still print.
To prevent code to continue you could insert throw exception after Debug.Break(). Such as "throw new Exception( )"
Code will spring out as if there is error on throw line.
Answer by Wappenull · Jul 11, 2020 at 07:26 AM
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)