Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    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,247
    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,247
    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,677
    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.
     
    Harry-Wells likes this.
  6. kloot

    kloot

    Joined:
    Mar 14, 2018
    Posts:
    78
    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:
    325
    Toggling the exception on and off did the trick for me