Search Unity

  1. Improved Prefab workflow (includes Nested Prefabs!), 2D isometric Tilemap and more! Get the 2018.3 Beta now.
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. Let us know a bit about your interests, and if you'd like to become more directly involved. Take our survey!
    Dismiss Notice
  4. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice
  5. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

Why is the Controller Returning Null Reference Exception?

Discussion in 'Scripting' started by FGPArthurVII, Jun 14, 2018.

  1. FGPArthurVII

    FGPArthurVII

    Joined:
    Jan 5, 2015
    Posts:
    66
    In the Console it Says:
    NullReferenceException: Object reference not set to an instance of an object
    ExtinguisherJet.Bursting () (at Assets/Scripts/ExtinguisherJet.cs:76)
    ExtinguisherJet.Update () (at Assets/Scripts/ExtinguisherJet.cs:113)

    Note: This is the line where I set It to work with the Oculus Touch (Oculus Rift's Controller). But with the B key I've set to the Keyboard It does not return any error and works perfectly.

    Note2: I'm Using SteamVR.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class ExtinguisherJet : MonoBehaviour
    6. {
    7.     private SteamVR_Controller.Device controller;
    8.     private Transform heldObj;
    9.     private GameObject jet;
    10.     private Animator anim;
    11.     [SerializeField] private GameObject[] extrs;
    12.     [SerializeField] private Transform fbHand;
    13.     private string lasMessage;
    14.     private bool lookForExtinguisher ()
    15.     {
    16.         bool isThere = false;
    17.  
    18.         try
    19.         {
    20.             heldObj = fbHand.transform.GetChild(2);
    21.             isThere = true;
    22.         }
    23.         catch (System.Exception e)
    24.         {
    25.             heldObj = null;
    26.             isThere = false;
    27.         }
    28.         return isThere;
    29.     }
    30.     //--------------------------------------------//
    31.  
    32.     void Start()
    33.     {
    34.         lasMessage = "";
    35.         jet = null;
    36.         anim = null;
    37.     }
    38.  
    39.     private void CheckHand ()
    40.     {
    41.         if (lookForExtinguisher () && lasMessage != heldObj.name)  // lasMessage is Just to Keep the console from printing the same stuff over and over
    42.         {
    43.             print ("Yo, check It out, I got some" + " " + heldObj.name);
    44.  
    45.             if (heldObj.name == "Extinguisher")
    46.             {
    47.                 jet = heldObj.gameObject;
    48.                 for (int i = 1; i < 20; i++)
    49.                     jet = jet.transform.GetChild(0).gameObject;
    50.  
    51.                 print ("Found " + jet.name + " in " + heldObj.name);
    52.                 anim = heldObj.GetComponent <Animator>();
    53.                 anim.SetBool ("willAim", true);
    54.             }
    55.  
    56.             lasMessage = heldObj.name;
    57.  
    58.         } else if (!lookForExtinguisher () && lasMessage != "nothing") {
    59.             print ("Ma boey! Derr's nothin' on yo hands!");
    60.             lasMessage = "nothing";
    61.  
    62.             if (anim != null)
    63.                 anim.SetBool("willAim", false);
    64.         }
    65.     }
    66.  
    67.     private void Bursting ()
    68.     {
    69.         if (lookForExtinguisher ())
    70.         {
    71.             if (heldObj.name == "Extinguisher" && Input.GetKey(KeyCode.B))
    72.             {
    73.                 print ("I'm Bursting");
    74.                 jet.SetActive (true);
    75.             }
    76.             else if (heldObj.name == "Extinguisher" && controller.GetPressDown(SteamVR_Controller.ButtonMask.Trigger))
    77.             {
    78.                 print ("I'm Bursting");
    79.                 jet.SetActive(true);
    80.             }
    81.             else if (jet != null)
    82.             {
    83.                 jet.SetActive(false);
    84.             }
    85.         }
    86.     }
    87.  
    88.     private void SearchforBursting ()
    89.     {
    90.         GameObject tempObj = null;
    91.         GameObject tempJt = null;
    92.         int i = 0;
    93.         int j = 0;
    94.         for (i = 0; i < extrs.Length; i++)
    95.         {
    96.             tempObj = extrs [i];
    97.             tempJt = tempObj;
    98.             for (j = 1; j < 20; j++)
    99.                 tempJt = tempJt.transform.GetChild(0).gameObject;
    100.            
    101.             if (tempJt.activeSelf && heldObj != tempObj.transform)
    102.             {
    103.                 tempJt.SetActive(false);
    104.                 print ("Deactivated");
    105.                 print (heldObj + ", " + tempObj);
    106.             }
    107.         }
    108.     }
    109.  
    110.     void Update ()
    111.     {
    112.         CheckHand ();
    113.         Bursting ();
    114.         SearchforBursting ();
    115.     }
    116. }
    Thanks;
    Arthur.
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    3,976
    Then my guess is controller is null. I don't see you assigning anything to it in this script.
     
  3. FGPArthurVII

    FGPArthurVII

    Joined:
    Jan 5, 2015
    Posts:
    66
    What you mean? It serves to print "bursting" and activating the jet, just like It's done to the KeyCode.B Above it
     
  4. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,395
    Have you tried putting a breakpoint on line 76 to see which variable is null? That might be a good starting point to work from.
     
  5. FGPArthurVII

    FGPArthurVII

    Joined:
    Jan 5, 2015
    Posts:
    66
    Brea
    Breakpoint? What is that?
     
  6. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,395
    Are you using monodevelop or Visual Studio?
     
  7. FGPArthurVII

    FGPArthurVII

    Joined:
    Jan 5, 2015
    Posts:
    66
    monodevelop
     
  8. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    3,976
    You seem pretty new to programming...

    Code (CSharp):
    1. private SteamVR_Controller.Device controller;
    This line creates a variable.

    But you never assign anything to it, which makes it null.

    Code (CSharp):
    1.  controller.GetPressDown(SteamVR_Controller.ButtonMask.Trigger)
    So when you call this line, you get a null reference error. controller has no value. It's null.

    Where do you assign something to controller?

    controller = something
     
    Doug_B likes this.
  9. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,395
    In that case, you may want to directly consider the suggestion by Brathnann which would seem to be the answer you are looking for.

    I would, however, recommend moving over to Visual Studio. The debugger is exceptionally useful and would have shown you to the solution here very quickly. :)

    This is because the code flow does not reach the offending line in that case.