Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Question Visual studio - break on exception

Discussion in 'Code Editors & IDEs' started by andyz, Aug 11, 2021.

  1. andyz

    andyz

    Joined:
    Jan 5, 2010
    Posts:
    2,297
    I have no problem breaking VS on placed break points when debugging but if unity code throws an exception then Unity can be made to pause the game and show where the error occurred in the console...
    but I want it to switch to VS and show the current call stack, variable values etc. - should I be able to?

    As it is, I have to repeat the actions with a new breakpoint at the point of exception to get a proper look at the error in VS, seems a Unity limitation
     
  2. Neto_Kokku

    Neto_Kokku

    Joined:
    Feb 15, 2018
    Posts:
    1,751
    In Visual Studio, go into Debug / Windows / Exception Setting, then enable the managed exceptions you want the debugger to break on.
     
    skullthug and andyz like this.
  3. andyz

    andyz

    Joined:
    Jan 5, 2010
    Posts:
    2,297
    Thanks! Not sure why I do not know this, but could be mentioned in Unity debugging notes
     
  4. DragonCoder

    DragonCoder

    Joined:
    Jul 3, 2015
    Posts:
    1,745
    Sorry to necro this, but is the mentioned setting above really the solution?

    Would also like that when the NullReferenceException occurs (unintialized variable etc.), the debugger should stop to show me which variable is the culprit (since the error log does only provide the line and sometimes have multiple reference sin the same line).

    However despite this (which already was enabled) no exceptions are thrown.
    upload_2022-8-22_0-51-16.png

    Breakpoints do work, so the "Attach to Unity" part is successful.

    VS version: 2022.

    Any ideas?
     
  5. Neto_Kokku

    Neto_Kokku

    Joined:
    Feb 15, 2018
    Posts:
    1,751
    There's a separate, Unity-specific exception called "MissingReferenceException" which is thrown in the Editor when a reference to a Unity object is a "fake null".

    Also, sometimes I have to toggle the exception off then on again after attaching the debugger for it to stick.
     
    Anthiese likes this.
  6. kloot

    kloot

    Joined:
    Mar 14, 2018
    Posts:
    79
    For folks wondering how to add the MissingReferenceException in VS:
    Right click "Common Language Runtime Exceptions" in the screenshot above, hit the plus-icon and add "UnityEngine.MissingReferenceException"

    And yes, I've also seen a lot of flakyness with getting this to work reliably like needing to toggle exceptions on and off to get it to work (never had a problem with any other targets than Unity).
     
    horeaper and Maeslezo like this.
  7. Maeslezo

    Maeslezo

    Joined:
    Jun 16, 2015
    Posts:
    347
    Toggling the exception on and off did the trick for me