- Home /
Debug.Break() ; not working properly
I have this code
Debug.Break() ;
print("Break was ignored")
When I run the script " I see Break was ignored" in the console. I am trying to test my variables and I am unable to test them since I get the results back that was posted after the debug break. I have another line after print and the whole script gets called, so I am unable to check my variables before the break was issued.
Debug.Break() ;
print("Break was ignored");
EditGridColor(thePath);
Do I under stand Break wrong? I am trying to pause the app so I can see the where I am currently at and why my results are not how they should be.
also if I have 2 Breaks, the app will only break one, so I can't break, continue, break, continue. I will only do break continue, does not matter how many breaks you have, even if you have like 10 breaks.
Debug.Break() ;
print("Break was ignored");
EditGridColor(thePath);
Debug.Break() ;
print("This Break was totally ignored");
P.S. After Debug.Break(), all the scripts ran and then game paused.
As a workaround, you can use the $$anonymous$$onodevelop degbugger.
Answer by Bunny83 · Nov 17, 2014 at 05:01 PM
Debug.Break just pauses the editor when the current frame is finished. It's just like you clicked on the pause button manually but you can trigger it at a specific frame. If you want to do code analysis you have to use the debugger like robertbu said. There you can set break points on every line you want.
edit
Unfortunately the unitygems site seems to be hacked / offline for some weeks now. Here's Unity's documentation on the debugger.
Answer by liveswithoutfear · Mar 29, 2021 at 10:37 AM
I know this is an older thread but I thought I could weigh in, in case someone else comes across this and it might help. If you want to pause the editor and stop the rest of the code from running then after Debug.Break(); just add return; This will stop the rest of the code running in that script, it'll become obvious because in visual studio the code underneath "return;" will become dim. It won't, however, stop the rest of the code in other scripts from running at the moment return is called, every other script will still finish its update frame.
Answer by Venryx · Sep 11, 2016 at 02:38 PM
Here is a solution that gets basically what you want:
1) Copy the System.Windows.Forms.dll file into your Assets folder somewhere. (you can find it here: C:\Program Files\Unity\Editor\Data\Mono\lib\mono\2.0)
2) Add these lines in place of the "Debug.Break();" line:
MessageBox.Show("Something happened. Attach the debugger, add a breakpoint to the line after this one, then press OK.");
"does nothing".ToString(); // place breakpoint here
Now when that area of code is hit, the message-box will show, and will block the code execution. Then just add the breakpoint to the line after (as it says), press OK, and the code will resume and hit the breakpoint you just added.
It's a bit more work than just a BreakHere(); call, but it accomplishes the same thing.
Edit
==========
For those who don't want to add a DLL to their project, you can also just call the User32.dll's MessageBox method directly.
Here's a paste-bin showing the working code for this variant: http://pastebin.com/JF5RVG6Y
In this case, you'd call "Assert(false);" from the line where you want to break. (then just use the "Call stack" window to go up one method and inspect the state)
Doesnt work... $$anonymous$$essageBox is being highlighted as error. DO you use it with Visual Studio?
I think I did use it in Visual Studio, yes. However, even if it shows an error in VIsual Studio, it should still be able to compile in Unity (different compiler chain). So try making the change anyway, and seeing if it works.
If not, then are you sure you're using the exact DLL I referenced? If you are, then it may just be that they updated the code somewhere -- I haven't used Unity in months so I don't know if this approach works with the latest versions. (I know it worked earlier since I relied on it heavily for certain debugging tasks)
Answer by kornica3d · Feb 08, 2018 at 03:31 PM
Even i figured out u have to do System.Windows.Forms.MessageBox, still window shows but as i click okay nothing is happening the game keeps on going afterwards