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

Resolved "Object reference not set to an instance of an object" when using raycast

Discussion in 'Scripting' started by barrlin, Jul 17, 2023.

  1. barrlin

    barrlin

    Joined:
    Jun 29, 2021
    Posts:
    8
    "NullReferenceException: Object reference not set to an instance of an object
    PlayerMove.Update () (at Assets/PlayerScripts/PlayerMove.cs:85)"

    Yes, that should be easy to figure out right?

    I am trying to fire a raycast with a mouse click. Upon clicking the designated key, I get the above error. I get the same error if there is no "if" statement. The main camera is a grandchild of the "Player" which this script is a component of. It goes Player > Cam Axis > Main Camera. Here is the relevant section of script. I'm sure it's something stupid but I don't even know where to start on this one.

    Code (CSharp):
    1.     public CharacterController controller;
    2.     private Animator Anim;
    3.     Camera cam; //<<<THIS IS RELATED TO THE LINE WITH THE ERROR
    4.  
    5.  
    6.  
    7.     private void Start()
    8.     {
    9.         controller = GetComponent<CharacterController>();
    10.         Anim = GetComponentInChildren<Animator>();
    11.         cam = Camera.main;  //<<<THIS IS RELATED TO THE LINE WITH THE ERROR
    12.  
    13.  
    14.     }
    15.  
    16.     private void Update()
    17.     {   //transform.LookAt(target);
    18.  
    19.               moveDirection = new Vector3 (0, 0, moveSpeed);
    20.      //moveDirection = moving along 0x, 0y, (1, 0 or -1f)z
    21.      moveDirection = transform.TransformDirection(moveDirection);
    22.  
    23.         if(!Input.GetKey(KeyCode.Mouse0) && !Input.GetKey(KeyCode.Mouse0))
    24.         {
    25.             CameraNone();
    26.             Idle();
    27.         }
    28.         if(Input.GetKey(KeyCode.Mouse0) && !Input.GetKey(KeyCode.Mouse1))
    29.         {
    30.             CameraRot();
    31.         }
    32.         if(!Input.GetKey(KeyCode.Mouse0) && Input.GetKey(KeyCode.Mouse1))
    33.         {
    34.             PlayerRot();
    35.         }
    36.         else if(Input.GetKey(KeyCode.Mouse0) && Input.GetKey(KeyCode.Mouse1))
    37.         {
    38.             Move();
    39.             //Debug.Log("000939030");
    40.         }
    41.         if(moveDirection == Vector3.zero && Input.GetKey(KeyCode.Mouse1)) //<<<THE ERROR ARISES WITHIN THIS STATEMENT
    42.         {
    43.             Debug.Log("Mouseclick");
    44.             Ray ray = cam.ScreenPointToRay(Input.mousePosition);  //<<THIS IS THE LINE THROWING THE ERROR, LINE 85
    45.             RaycastHit hit;
    46.  
    47.             if(Physics.Raycast(ray, out hit))
    48.             {
    49.                 Debug.Log("Raycast hit");
    50.             }
    51.  
    52.         }
    53.  
    54.  
    55.     }
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,140
    This is a essentially a null ref error.

    1. Find out what is null
    2. Find out why it's null.
    3. Fix it.

    So, the error says line 85, which you point out is line 44. At first glance, I feel like cam is null. But, you should add a debug right before line 44 to print out the value of cam.

    If it's null, then in Start, you are trying to assign the value to Camera.main.
    Camera.main will return the camera that is assigned the MainCamera tag. So, make sure you have a camera that has that tag.

    If it's not null, then you need to add some debug calls and see what is null in that line.
     
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,563
    Yep, get on it now. Nobody here can help.

    The answer is always the same... ALWAYS!

    How to fix a NullReferenceException error

    https://forum.unity.com/threads/how-to-fix-a-nullreferenceexception-error.1230297/

    Three steps to success:
    - Identify what is null <-- any other action taken before this step is WASTED TIME
    - Identify why it is null
    - Fix that
     
  4. barrlin

    barrlin

    Joined:
    Jun 29, 2021
    Posts:
    8
    Somehow my MainCamera got untagged lol.

    Tagging my MainCamera as MainCamera fixed it.
     
  5. barrlin

    barrlin

    Joined:
    Jun 29, 2021
    Posts:
    8

    It was the "relevant section of the script." I doubt you all would want to look through 400 lines of code when 3 lines are where the problem was occurring.
     
  6. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,140
    Actually, we usually don't mind seeing the entire code as long as code tags are used. Sometimes errors aren't where we think they are. Or, they can be caused by something else.
     
    barrlin likes this.