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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Guys I'm getting a NullReferenceException pls help me

Discussion in 'Editor & General Support' started by ShaunProg, Nov 11, 2021.

  1. ShaunProg

    ShaunProg

    Joined:
    Dec 21, 2020
    Posts:
    4
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. class Gun : MonoBehaviour
    4. {
    5.  
    6.     public float damage;
    7.     public float range = 100f;
    8.  
    9.     public GameObject fpsCam;
    10.  
    11.     // Update is called once per frame
    12.     void Update()
    13.     {
    14.        
    15.     }
    16.  
    17.     public void Shoot()
    18.     {
    19.         RaycastHit hit;
    20.         if (Physics.Raycast(fpsCam.transform.position, fpsCam.transform.forward, out hit, range))
    21.         {
    22.             Debug.Log(hit.transform.name);
    23.  
    24.             Target target = hit.transform.GetComponent<Target>();
    25.             if (target != null)
    26.             {
    27.                 target.TakeDamage(damage);
    28.             }
    29.         }
    30.     }
    31. }
    32.  
    It says there is a NullReferenceException in line 20 of this file and it has 2 other references. The error isn't showing in my IDE, but when i left click after holding a weapon it suddenly stops and shows the error. I'll give code of my Items Controller as well. Idk if its a bug or not.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class ItemsController : MonoBehaviour
    6. {
    7.  
    8.     public Transform handsPosition;
    9.     public float distance = 10f;
    10.  
    11.     GameObject currentItem;
    12.     GameObject item;
    13.     bool canGrab;
    14.  
    15.     // Start is called before the first frame update
    16.     void Start()
    17.     {
    18.        
    19.     }
    20.  
    21.     // Update is called once per frame
    22.     void Update()
    23.     {
    24.  
    25.         if (Input.GetButtonDown("Fire1"))
    26.         {
    27.             ConfirmShoot();
    28.         }
    29.  
    30.         CheckItem();
    31.  
    32.         if (canGrab)
    33.         {
    34.             if (Input.GetKeyDown(KeyCode.E))
    35.             {
    36.                 if (currentItem != null)
    37.                 {
    38.                     Drop();
    39.                 }
    40.                 Pickup();
    41.             }
    42.         }
    43.         if (currentItem != null)
    44.         {
    45.             if (Input.GetKeyDown(KeyCode.Q))
    46.             {
    47.                 Drop();
    48.             }
    49.         }
    50.     }
    51.  
    52.     void CheckItem()
    53.     {
    54.         RaycastHit hit;
    55.         if (Physics.Raycast(Camera.main.transform.position,Camera.main.transform.forward,out hit, distance))
    56.         {
    57.             if (hit.transform.tag == "Damagable")
    58.             {
    59.                 canGrab = true;
    60.                 item = hit.transform.gameObject;
    61.             }
    62.             else
    63.                 canGrab = false;
    64.         }
    65.     }
    66.  
    67.     void Pickup()
    68.     {
    69.         currentItem = item;
    70.         currentItem.transform.parent = handsPosition;
    71.         currentItem.transform.position = handsPosition.position;
    72.         currentItem.transform.localEulerAngles = handsPosition.localPosition;
    73.         currentItem.GetComponent<Rigidbody>().isKinematic = true;
    74.         currentItem.GetComponent<MeshCollider>().enabled = false;
    75.     }
    76.     void Drop()
    77.     {
    78.         currentItem.transform.parent = null;
    79.         currentItem.GetComponent<Rigidbody>().isKinematic = false;
    80.         currentItem.GetComponent<MeshCollider>().enabled = true;
    81.         currentItem = null;
    82.     }
    83.  
    84.     void ConfirmShoot()
    85.     {
    86.         if (currentItem != null)
    87.         {
    88.             if (currentItem.tag == "Damagable")
    89.             {
    90.                 Gun gun = new Gun();
    91.                 gun.Shoot();
    92.             }
    93.         }
    94.     }
    95.    
    96. }
    97.  
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,971
    The answer is always the same... ALWAYS. It is the single most common error ever.

    Don't waste your life spinning around and round on this error. Instead, learn how to fix it fast... it's EASY!!

    Some notes on how to fix a NullReferenceException error in Unity3D
    - also known as: Unassigned Reference Exception
    - also known as: Missing Reference Exception
    - also known as: Object reference not set to an instance of an object

    http://plbm.com/?p=221

    The basic steps outlined above are:
    - Identify what is null
    - Identify why it is null
    - Fix that.

    Expect to see this error a LOT. It's easily the most common thing to do when working. Learn how to fix it rapidly. It's easy. See the above link for more tips.

    You need to figure out HOW that variable is supposed to get its initial value. There are many ways in Unity. In order of likelihood, it might be ONE of the following:

    - drag it in using the inspector
    - code inside this script initializes it
    - some OTHER external code initializes it
    - ? something else?

    This is the kind of mindset and thinking process you need to bring to this problem:

    https://forum.unity.com/threads/why-do-my-music-ignore-the-sliders.993849/#post-6453695

    Step by step, break it down, find the problem.

    Here is a clean analogy of the actual underlying problem of a null reference exception:

    https://forum.unity.com/threads/nul...n-instance-of-an-object.1108865/#post-7137032
     
  3. ShaunProg

    ShaunProg

    Joined:
    Dec 21, 2020
    Posts:
    4
    Im getting the object reference not set to an instance of an object. I have filled the require fields in inspector. Earlier it was working. The error is on line 20 of first script. I checked many websites but still cant find solution. I will try by assigning the fpsCam gameObject in script itself rather than assigning it in the inspector.
     
    Last edited: Nov 12, 2021
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,971
    Make sure the script isn't inadvertently on another GameObject as well, and not properly filled out.
     
  5. ShaunProg

    ShaunProg

    Joined:
    Dec 21, 2020
    Posts:
    4
    No I have put the gun script in 2 gameObjects and filled the fields in inspector
    And also i dont understand what object is null i think im pretty sure i dont have anything null.
    I got it that my fpsCam is null but i assigned it in inspector and still its showing error
     
    Last edited: Nov 12, 2021