0
\$\begingroup\$

In my scripts, I have several "Debug.Break();".

I use them to halt the execution of the script when unexpected situations occur, for example like here:

 if (condition == 1) 
 {
    DoThis();
 }
 else if (condition == 2)
 {
    DoThat();
 }
 else
 {
     //this should not happen! Check what goes on here
     Debug.Break();
 }

When my game hits such a "Debug.Break()", the gameplay is automatically paused.

It looks like this:

enter image description here

Ideally, I would have the Debugger attached, and I would have a breakpoint set, then Visual Studio would show me exactely where it stopped.

It would look like this:

enter image description here

However, I don't have breakpoints at all Debug.Break(); and sometimes I do not even have the Debugger attached to Unity.

It would look like that:

enter image description here

enter image description here

How could I anyways still step into the line where Unity halted? I would like to see where exactely Unity halted, but I don't see how.

Do I always have to have breakpoints set and the Debugger attached? Or could I still attach the debugger later and still step into where the script halted?

The reason why I don't have the Debugger attached at all time is that I frequently change the scripts, then I need to save it and detach and re-attach the Debugger. This is too time-taking.

I am using Visual Studio Community Edition.

\$\endgroup\$
2
  • \$\begingroup\$ Stepping into your code is what breakpoints are for, so the engine/IDE already gives you a tool that accomplishes this. \$\endgroup\$
    – DMGregory
    Commented Aug 4, 2021 at 11:23
  • \$\begingroup\$ @DMGregory I have yet again re-phrased my question because I think it was not yet clear. \$\endgroup\$
    – tmighty
    Commented Aug 4, 2021 at 12:10

3 Answers 3

1
\$\begingroup\$

How could I anyways still step into the line where Unity halted? I would like to see where exactely Unity halted, but I don't see how.

Do I always have to have breakpoints set and the Debugger attached?

Yes.

The reason why I don't have the Debugger attached at all time is that I frequently change the scripts, then I need to save it and detach and re-attach the Debugger.

You should not need to detach and re-attach the debugger each time you make a change to a script. If the debugger is detaching itself, then you should try updating to the latest versions of Unity and Visual Studio. If you're manually detaching the debugger, just stop doing that.

\$\endgroup\$
1
  • \$\begingroup\$ You are right. I don't have to detach the debugger each time I change my script. \$\endgroup\$
    – tmighty
    Commented Aug 4, 2021 at 22:43
1
\$\begingroup\$

This answer is for the original question; the asker completely rewrote the question into a new question.

Debug.Break() is useful if you need to look at what's going on in a scene at a specific point in time. Some example use cases:

  • An character occasionally walks through a wall. When this happens, you want to pause the editor to check the character's speed, waypoints, and current animation.
  • You have a coroutine which checks every 30 seconds for characters that are stuck and moves them until they're un-stuck. You want to see the before-and-after, so you set a Debug.Break() to pause the engine when 30 seconds have gone by and the coroutine is about to un-stick the characters.
  • OnCollisionEnter() is occasionally getting called when you don't expect it, so you use Debug.Break() to pause the engine when a collision occurs so you can see what's causing the collision.

Any time that you need to pause the engine to examine the scene, but it's difficult or impossible to manually hit the pause button at exactly the right time, use Debug.Break().

\$\endgroup\$
3
  • \$\begingroup\$ Thank you. I agree. However, I do not see how I could step into my code when the Pause occurs. I would have to have breakpoints on all "Debug.Break();" in my code to see where it currently paused, correct? \$\endgroup\$
    – tmighty
    Commented Aug 4, 2021 at 9:34
  • \$\begingroup\$ I have yet again re-phrased my question because I think it was not yet clear. \$\endgroup\$
    – tmighty
    Commented Aug 4, 2021 at 12:10
  • 1
    \$\begingroup\$ @tmighty You didn't "re-phrase" it, you turned it into a completely different question. \$\endgroup\$
    – Kevin
    Commented Aug 4, 2021 at 22:24
0
\$\begingroup\$

If you look at Debug.log() in the official docs, it takes context as its second parameter, you can pass your Gameobject in that to know which object is calling the debug statement then call your Debug.break() to pause the scene.

Debug.log("myMessage",myGameObject);

Debug.break();
\$\endgroup\$
1
  • \$\begingroup\$ I have yet again re-phrased my question because I think it was not yet clear. \$\endgroup\$
    – tmighty
    Commented Aug 4, 2021 at 12:10

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .