Search Unity

Bug "NullReferenceException: Object reference not set to an instance of an object" Error

Discussion in 'Scripting' started by LG21fanisone, Jan 23, 2023.

  1. LG21fanisone

    LG21fanisone

    Joined:
    Jan 23, 2023
    Posts:
    1
    Hello, I am working on a game using a tutorial that is kind of old (eight years ago) just for learning and practice purposes, a light gun shooter. The tutorial I am using is from a youtuber PushyPixels, so credits go to him.

    Video:


    Most of the code I tried out was working up until the clicking in other places, which cause this error:

    NullReferenceException: Object reference not set to an instance of an object
    ShotPOS.Update () (at Assets/Scripts/ShotPOS.cs:26)

    Here is my code:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class ShotPOS : MonoBehaviour
    6. {
    7.  
    8.     public Rigidbody bullet;
    9.     public float force = 10.0f;
    10.     public ForceMode forceMode;
    11.  
    12.  
    13.     // Start is called before the first frame update
    14.     void Start()
    15.     {
    16.        
    17.     }
    18.  
    19.     // Update is called once per frame
    20.     void Update()
    21.     {
    22.  
    23.         if (Input.GetMouseButtonDown(0))
    24.  
    25.         {
    26.             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    27.             Vector3 spawnPosition = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, Camera.main.nearClipPlane + 1.0f));
    28.  
    29.  
    30.  
    31.             Rigidbody instance = Instantiate(bullet, transform.position, Quaternion.LookRotation(ray.direction)) as Rigidbody;
    32.             instance.AddForce(transform.forward * force, forceMode);
    33.         }
    34.    
    35.    
    36.     }
    37. }
    The
    Code (CSharp):
    1.  Vector3 spawnPosition = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, Camera.main.nearClipPlane + 1.0f));
    is what's causing the issue. If anyone can please help, let me know.

    And yes, I am very new to this, just using tutorials to learn how most things work.
     
  2. jontefasth22

    jontefasth22

    Joined:
    Jan 22, 2018
    Posts:
    1
    Make sure that the camera in your scene is assigned to the "MainCamera" tag. Calling Camera.main is essentially the same as calling GameObject.FindObjectWithTag("MainCamera").

    Which means that if there is no camera in your scene with the "MainCamera" tag then Camera.main will not return a camera and thus you get a null reference exception.
     
    chemicalcrux likes this.
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,697
    It actually does not even remotely matter what you are doing, and you actually never need to post for a nullref!

    The reason is 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