Search Unity

NullReferenceExeption: Object Reference not set as an instance of an Object

Discussion in 'AR' started by pradyumnp508, Aug 31, 2020.

  1. pradyumnp508

    pradyumnp508

    Joined:
    Aug 9, 2020
    Posts:
    15
    Hello,
    I know this may seem a very common problem and some may say that the solution to this is already available in many forums, BUT, I have tried them but i still get the issue.

    Here's what I am trying to accomplish: I want to make my cube jump, when the UI Button for jump is pressed,

    To do so, I have 2 scripts, 1. Fall_Controll2 and 2. E_Jump.
    In Fall_Controll, I basically Checks if the Cube is grounded or not, and based on if its grounded or not, it sets the boolean value of "grounded" true or false. The script also checks if the object has fallen off the detected ARPlanes and if so it places the Cube back to the origin. Here the Script Fall_Controll2:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Fall_Control_2 : MonoBehaviour
    6. {
    7.     GameObject Soldier;
    8.     RaycastHit hit;
    9.     float distance;
    10.     Vector3 lastValidPos;
    11.  
    12.     public bool grounded;
    13.  
    14.     void Start()
    15.     {
    16.         Soldier = GameObject.FindGameObjectWithTag("Player");
    17.     }
    18.  
    19.    
    20.     void FixedUpdate()
    21.     {
    22.         if (Physics.Raycast(Soldier.transform.position, -Vector3.up, out hit))
    23.         {
    24.             lastValidPos = hit.transform.position;
    25.             //Debug.Log(hit.collider.gameObject.tag);
    26.  
    27.             //Debug.Log(hit.distance);
    28.             if (hit.collider.gameObject.tag == "Ground")
    29.             {
    30.                 if (hit.distance <= 0.1)
    31.                 {
    32.                     Debug.Log("On Ground");
    33.                     grounded = true;
    34.                 }
    35.  
    36.                 else if (hit.distance > 0.1)
    37.                 {
    38.                     Debug.Log("In Air");
    39.                     grounded = false;
    40.                 }
    41.             }
    42.  
    43.             else if (hit.collider.tag == "null")
    44.             {
    45.                 Debug.Log("WE In The Void BOII");
    46.             }
    47.            
    48.  
    49.         }
    50.  
    51.         else
    52.         {
    53.             Debug.Log("WE In The Void BOII");
    54.             //Soldier.transform.position = new Vector3(lastValidPos.x, lastValidPos.y + 0.5f, lastValidPos.y);
    55.             Soldier.transform.position = new Vector3(0f, 0.5f, 0f);
    56.         }
    57.  
    58.        
    59.     }
    60. }
    61.  
    And then, I have another script for the "Jump Button", called E_Jump.cs. Here I simply take the reference of the grounded variable, and use it inside the function E_jump(), to check if the cube is grounded, and log the same in console. Here's the Script:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class E_Jump : MonoBehaviour
    6. {
    7.    
    8.     Fall_Control_2 fall_Control;
    9.  
    10.     // Start is called before the first frame update
    11.     void Start()
    12.     {
    13.     }
    14.  
    15.     // Update is called once per frame
    16.     void Update()
    17.     {
    18.      
    19.     }
    20.  
    21.     public void E_jump()
    22.     {
    23.         if (fall_Control.grounded)
    24.         {
    25.             Debug.Log("I Jumped Once");
    26.         }      
    27.     }
    28. }
    29.  
    The reason I have 2 scripts is because I had to use FixedUpdate() for the physics and Raycasting( Because I read that we should use fixed update for physics and stuffs), and if I define the E_Jump Function, in a fixed Update, then it sometimes does't registers the button presses(Which may be because the scene is updated fixed amount of times when using fixed update).

    Whenever I call "grounded" in my E_jump script it throws null Exception. I also tried making an accessor function, but that too didnt worked. Can anyone point out where I messed up? Thanks in advance.

    Unity Version: 2019.3
    ARFoundationL 4.1.0
    Annotation 2020-08-31 143634.jpg
     
  2. lumoconnor

    lumoconnor

    Joined:
    Jan 17, 2016
    Posts:
    27
    So the error is occurring at line 23 in your E_Jump script, which is

    Code (CSharp):
    1.  if (fall_Control.grounded)
    The error means that fall_Control is NULL. It hasn’t been set! My guess is that you haven’t assigned fall_Control in the inspector.