visual-studio - unity3d - When running a Unity HoloLens program from Visual Studio, when an exception is thrown how do I get the line numbers in the stack trace? - answerstu - answerstu.com answerstu

unity3d - When running a Unity HoloLens program from Visual Studio, when an exception is thrown how do I get the line numbers in the stack trace?

Currently when an exception is thrown from within my Unity script while using my HoloLens the Debug Output in Visual Studio shows the stack trace without the line numbers.

How do I get the line numbers along with the stack trace? I'd be fine with it being logged somewhere else other than the Debug Output.

Here's some example output in Visual Studio:

Exception thrown: 'System.NullReferenceException' in Assembly-CSharp.dll
NullReferenceException: Object reference not set to an instance of an object.
   at NewBehaviourScript.Update()
   at NewBehaviourScript.$Invoke6Update(Int64 instance, Int64* args)
   at UnityEngine.Internal.$MethodUtility.InvokeMethod(Int64 instance, Int64* args, IntPtr method) 
(Filename: <Unknown> Line: 0)

And the corresponding Unity script (I made a Cube and attached a NewBehaviourScript component):

public class NewBehaviourScript : MonoBehaviour {           
    // Update is called once per frame
    void Update ()
    {
        object a = null;
        a.GetType();    
    }
}

I tried changing the build from Release to Debug doesn't give the line numbers.

I tried googling, and it looks like it's not showing the line numbers for others, as well: http://answers.unity3d.com/questions/1315985/null-reference-in-line-0.html

I tried asking on Microsoft's forums, but didn't receive any useful replies.

1 Answer

  1. Luke- Reply

    2019-11-13

    I don't think you would get the line number since it does not exist anymore. You get it in Unity editor because you are not running a full build of the application so Unity still has access to the non-compiled code. When you run on the device, it sends debug commands to the VS console about the printing and the errors but all the code is binary at that point, so there is no reason nor possibility to provide a line number.

    Actually this is not specific to Hololens, but you would get the same in Android or iOS. Once build, the code is no longer the same, it does not even match one to one as the compiler performs optimizations.

    What you can do is placed Debug commands to see where it happens.

    public class NewBehaviourScript : MonoBehaviour {           
        // Update is called once per frame
        void Update ()
        {
            object a = null;
    #if DEBUG
            if(a == null)
            { 
                Debug.Log("[NewBehaviourScript] Running update with null a object");
            }
    #endif 
            a.GetType(); 
            Debug.Log("[NewBeahviourScript] if this line prints, method did not crash");
        } 
    }
    

    In this example, you can use the DEBUG macros if you would have code running only for debug purpose. This way you can easily exclude it on export. The second call for Debug is not required in the macro since the build process will discard it when you set the build to Release or Master.

Leave a Reply

Your email address will not be published. Required fields are marked *

You can use these HTML tags and attributes <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>