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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

NullReferenceException when Loading scene

Discussion in 'Scripting' started by Myrmidou, Mar 15, 2018.

  1. Myrmidou

    Myrmidou

    Joined:
    Jun 7, 2014
    Posts:
    30
    Hi,

    First, please check the screenshot of my editor below.

    In my project I have 3 scenes: a main menu with a simple camera and 2 scenes with the PlayerVR2 prefab.
    This prefab has a child "SMI_CameraWithEyeTracking" with a SMI Eye Tracking Unity component. This SMI component is an imported dll.
    The screenshot is what happens when I try to load a scene with SMI for the 2nd time. The first scene works fine but even if I reload the same one it causes this issue.

    The issue is as you can see a triple NullReferenceException error, all of them refering to the PlayerVR2 prefab and its 2 children, all of them calling the SMI script at one point.

    Do you have an idea of what causes this issue?

    If you need anything please let me know.

    Thanks a lot!
     

    Attached Files:

  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,146
    Yes, you have a null reference. Since it's not really possible to know without seeing a script and a copy of the error (copied and not a screenshot) it's hard for us to tell you where. But the good thing about this error is it will tell you what script and which line. Usually you can also double click the error for it to take you to the line. Then you just have to ask yourself, what can be null on this line.

    If you aren't sure, start sticking debug.log stuff printing out the value of each possible null object (this can help narrow it down if you have multiple possible null objects)
     
  3. Myrmidou

    Myrmidou

    Joined:
    Jun 7, 2014
    Posts:
    30
    Here are the 3 errors
    Every line numbered contains a function from the SMI script accessible by the SMI component attached to the PlayerVR2's child.

    This is what I use to fetch the SMI script in a script attached to the PlayerVR2 gameobject:

    Code (CSharp):
    1.  
    2. //SMICamGO is the gameobject containing the SMI script
    3. public GameObject SMICamGO;
    4.     public SMI.SMIEyeTrackingUnity smiInstance;
    5.  
    6.     // Use this for initialization
    7.     void Start () {
    8.             smiInstance = SMICamGO.transform.GetComponent<SMIEyeTrackingUnity> ();
    from there, I call the instance in the script like I did in the LookPR one (3rd error):
    Code (CSharp):
    1.  SMI_GazeObject = smiInstance.smi_GetGazedObject();
     
  4. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,146
    All three errors end here

    SMIGazeController.smi_getRayFromGaze ()

    So you are getting some null reference in that method.