Search Unity

HELP.... Object reference not set

Discussion in 'Getting Started' started by hrohibil, May 10, 2021.

  1. hrohibil

    hrohibil

    Joined:
    Apr 17, 2021
    Posts:
    280
    Hello

    I was following a tutorial and had got to an issue now.
    I have animated sprites for the face expressions for each of his 3 simple moves.

    I am getting a error on line #46 in the console. That is the line
    Code (CSharp):
    1. idleFacevar.SetActive(true);
    NullReferenceException: Object refence not set to an instance of an object.


    Code (CSharp):
    1.  public GameObject runFacevar;
    2.     public GameObject jumpFacevar;
    3.     public GameObject idleFacevar;
    4.  
    5.     // Start is called before the first frame update
    6.     void Start()
    7.     {
    8.         controller = GetComponent<CharacterController>();
    9.         anim = gameObject.GetComponentInChildren<Animator>();
    10.  
    11.         runFacevar = GameObject.Find("/Player_idle_01/Armature/root/Spine/chest/Neck/Head/GEO_Faceidle");
    12.         jumpFacevar = GameObject.Find("/Player_idle_01/GEO_FACE_Run");
    13.         idleFacevar = GameObject.Find("/Player_idle_01/GEO_Face_JUMP");
    14.     }
    15.  
    16.     // Update is called once per frame
    17.     void Update()
    18.     {
    19.         if (controller.isGrounded && Input.GetKey("up"))
    20.         {
    21.             anim.SetInteger("AnimPar", 1);
    22.  
    23.             runFacevar.SetActive(true);
    24.             idleFacevar.SetActive(false);
    25.             jumpFacevar.SetActive(false);
    26.         }
    27.         else if(controller.isGrounded)
    28.         {
    29.             anim.SetInteger("AnimPar", 0);
    30.  
    31.             runFacevar.SetActive(false);
    32.             idleFacevar.SetActive(true);
    33.             jumpFacevar.SetActive(false);
    34.  
    35.         }
    36.         if (Input.GetButton("Jump") && controller.isGrounded)
    37.         {
    38.             anim.SetInteger("AnimPar", 2);
    39.             moveDirection.y = jumpForce;
    40.  
    41.             runFacevar.SetActive(false);
    42.             idleFacevar.SetActive(false);
    43.             jumpFacevar.SetActive(true);
    44.         }
     
  2. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    Most of the time, a bug is some place that you're assuming something that isn't true. So, checking your assumptions is always a good idea.

    In this case, the error message leads you directly to the wrong assumption: on the line you point out, you're calling SetActive on idleFacevar, which assumes that idleFacevar is not null — but the error message tells you that it is.

    So back up: why did you think it should not be null? Well, you have code in the Start method that you think is setting it. So you are assuming (1) that that code is actually running, and (2) it is succeeding.

    Now's when you check your assumption by adding a Debug.Log. Insert the following right at the end of your Start() method:

    Code (csharp):
    1.  
    2.     if (idleFacevar == null) Debug.Log("Couldn't find GEO_Face_JUMP!!");
    3.     else Debug.Log("idleFacevar successfully set.");
    Now run, and look for these in the console. If neither message appears, either your Console is not showing messages, or the Start method is not running (which could mean this whole script is not running). If the first message appears, then your attempt to use GameObject.Find is not working. In that case you'll want to re-read the docs and double-check your path against what's actually in the scene.