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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

NullReferenceException: Object reference not set to an instance of an object

Discussion in 'Scripting' started by GeovaneUnity54, Jun 4, 2022.

  1. GeovaneUnity54

    GeovaneUnity54

    Joined:
    Nov 14, 2021
    Posts:
    91
    Good morning guys, so I wanted to know how to solve this error here:

    NullReferenceException: Object reference not set to an instance of an object Shooter Enemy.OnCollisionEnter2D (UnityEngine.Collision2D collisionShooter) (at Assets/Scripts/Sniper Enemy.cs:30)

    A guy named Kurt-Dekker always says that for errors like that, it's to identify what's null and then find out why it's null. But how do I identify what is null? Do you have any command for that?

    Here is my EnemySniper code:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class InimigoAtirador : MonoBehaviour
    6. {
    7.     public int VidaInimigoAtirador;
    8.  
    9.     public GameObject MuniçaoPlayer;
    10.     private Munição muniçaoplayer;
    11.  
    12.     public Transform Player;
    13.  
    14.     void Start()
    15.     {
    16.         muniçaoplayer = MuniçaoPlayer.GetComponent<Munição>();
    17.     }
    18.  
    19.     void Update()
    20.     {
    21.         Vector3 dir = Player.position - transform.position;
    22.         float angle = Mathf.Atan2(dir.y,dir.x) * Mathf.Rad2Deg;
    23.         transform.rotation = Quaternion.AngleAxis(angle -90, Vector3.forward);
    24.     }
    25.  
    26.     void OnCollisionEnter2D(Collision2D collisionAtirador)
    27.     {
    28.         if(collisionAtirador.gameObject.CompareTag("Muniçao"))
    29.         {
    30.             VidaInimigoAtirador -= muniçaoplayer.Dano;
    31.         }
    32.     }
    33. }
    Thank you very much in advance :)
     
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,557
    Look at the error you already have, it's telling you the exact line (30). You've got something that is NULL and you're trying to use it.

    There's only one reference (class) on that line which is "muniçaoplayer". It's NULL. You assign it in start assuming that a component is found. It obviously isn't being found.

    Simple stuff really.
     
    GeovaneUnity54 likes this.
  3. lordconstant

    lordconstant

    Joined:
    Jul 4, 2013
    Posts:
    389
    GeovaneUnity54 likes this.
  4. GeovaneUnity54

    GeovaneUnity54

    Joined:
    Nov 14, 2021
    Posts:
    91
    What is the attribution code?
     
  5. GeovaneUnity54

    GeovaneUnity54

    Joined:
    Nov 14, 2021
    Posts:
    91
    ok, i will give it a read
     
  6. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,557
    I don't understand the question, sorry.
     
    GeovaneUnity54 likes this.
  7. GeovaneUnity54

    GeovaneUnity54

    Joined:
    Nov 14, 2021
    Posts:
    91
    How do I assign the "muniçãoplayer"? Sorry I'm using Google Translate.
     
  8. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,557
    Code (CSharp):
    1. muniçaoplayer = MuniçaoPlayer.GetComponent<Munição>();
    This is how but if "MuniçaoPlayer" does not have a component of "Munição" then it will return NULL.

    A way to test this in code is:
    Code (CSharp):
    1.     void Start()
    2.     {
    3.         muniçaoplayer = MuniçaoPlayer.GetComponent<Munição>();
    4.         if (muniçaoplayer == null)
    5.             Debug.Log("Component NOT found!!");
    6.         else
    7.             Debug.Log("All good");
    8.     }