13
\$\begingroup\$

I'm trying to debug a Unity project using the Unity Editor and Visual Studio. A runtime exception keeps popping up, but the debugger doesn't halt when an exception occurs, making it impossible to debug my code efficiently.

In Visual Studio, I've gone into Tools -> Options -> Tools For Unity and set "Exception Support" to "True". I've also enabled all types of exceptions in the Exception Settings panel. I am connecting the debugger to Unity using the "Attach to Unity" button. Breakpoints are working, but the debugger isn't halting on Exceptions.

What do I need to do to get Unity/VS to actually halt on Exceptions?

Note: I've tried closing and re-opening both Unity and VS many times but that doesn't make any difference.

\$\endgroup\$
0
12
\$\begingroup\$

This documentation on the 2.0 Unity tools preview for VS implies you have to both enable Unity exception support via Tools -> Options -> Tools For Unity (setting "exception support" to true, as you've done) and enable the appropriate CLR exception masks in Visual Studio's built-in exception settings window (check the box next to "Common Language Runtime Exceptions").

For best result you may want to check and then un-check the CLR exceptions tick, as that will ensure all the exception subtypes are enabled to get started with.

\$\endgroup\$
0
3
\$\begingroup\$

You may need to do a couple more things to get this working. In my case, I wanted Unity's built-in Assertions to raise exceptions that would be caught by VS. I had to do the following to make this happen.

  1. Configure Unity's Assert class to raise exceptions via Assert.raiseExceptions=true
  2. Add the specific type of Exception to VS's exception settings window. (Right click, select Add, and input UnityEngine.Assertions.AssertionException)
\$\endgroup\$
3
  • \$\begingroup\$ Are there any other steps you took to get Unity Assertions to throw? These two steps don't seem to work on Unity 2014 LTS answers.unity.com/questions/1581028/… \$\endgroup\$
    – Jethro
    Dec 14 '18 at 12:14
  • 1
    \$\begingroup\$ No, however I've been using Unity 2018.x.x. Is it possible that older versions of Unity used a different Assertion type? (In which case, you'd need to add a slightly different qualified typename to that list). \$\endgroup\$ Jan 20 '19 at 13:10
  • 1
    \$\begingroup\$ Additionally, I noticed that in VS Community 15.9.2 - there was a new tickbox in this list for <All Common Language Runtime Exceptions not in this list> (Look under the Common Language Runtime Exceptions` category). If the exceptions you're attempting to catch from within Unity are not actually AssertionException's, then it's possible this tickbox might be inclusive enough to catch what you're after. \$\endgroup\$ Jan 20 '19 at 13:11
3
\$\begingroup\$

I think, a quick solution, you can try: Write an error log in exception block of the code

Debug.LogError("Exception occured");

And before running the editor open console and hit Error Pause in the console log. enter image description here

The game will pause as any error occurred in the console.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.