Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

NullReferenceException thrown when using HandleUtility.DistanceToPolyLine

Discussion in 'Editor & General Support' started by Florian-Nouviale, Feb 12, 2020.

  1. Florian-Nouviale

    Florian-Nouviale

    Joined:
    Mar 14, 2013
    Posts:
    51
    Hi!
    We have a custom editor window in our team that works well in Unity 2108.4
    I tried upgrading our project to 2019.3 in order to check if our custom editor would need some update for this version and I'm facing a weird error :

    Code (CSharp):
    1. NullReferenceException: Object reference not set to an instance of an object
    2. UnityEditor.CameraProjectionCache..ctor (UnityEngine.Camera camera, System.Single screenHeight) (at <6f28216fea9f453abf2e05b770ed3ee4>:0)
    3. UnityEditor.HandleUtility.DistanceToPolyLine (UnityEngine.Vector3[] points) (at <6f28216fea9f453abf2e05b770ed3ee4>:0)
    I've tried restarting my computer and Unity (that seemed to resolve related errors in previous versions from what I gathered on the internet) but no luck so far.
     
  2. MajorWolph

    MajorWolph

    Joined:
    Apr 28, 2018
    Posts:
    19
    Any chance you managed to find a fix for this issue in the end?
     
  3. Florian-Nouviale

    Florian-Nouviale

    Joined:
    Mar 14, 2013
    Posts:
    51
    As a follow up, we ended using the old implementation of the method in our code :

    Code (CSharp):
    1.         // We use the old implementation. Changed in Unity 2019.3 and throwing NullReferenceExceptions
    2.         public static float DistanceToPolyLine(params Vector3[] points)
    3.         {
    4.             if (points == null)
    5.                 throw new ArgumentNullException(nameof(points));
    6.             float dist = HandleUtility.DistanceToLine(points[0], points[1]);
    7.             for (int i = 2; i < points.Length; i++)
    8.             {
    9.                 float d = HandleUtility.DistanceToLine(points[i - 1], points[i]);
    10.                 if (d < dist)
    11.                     dist = d;
    12.             }
    13.             return dist;
    14.         }