Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Resolved Object reference not set to an instance of an object

Discussion in 'Scripting' started by robhertino, Apr 19, 2021.

  1. robhertino

    robhertino

    Joined:
    Feb 2, 2021
    Posts:
    4
    I was following along a video on how to make enemies take away lives but I've encountered a problem, It keeps giving me the error that says
    "NullReferenceException: Object reference not set to an instance of an object
    movement.OnTriggerEnter2D (UnityEngine.Collider2D other) (at Assets/movement.cs:67)"
    It seems that the livesCounter-- is not working and I cant understand why?

    I cannot seem to find why it is doing that, there are 3 scripts that link to the livesmanager which are the gamemanager and the movement

    theLM references the Lives manager
    theGM references the GameManager

    This is the Lives Manager
    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using UnityEngine.UI;
    6.  
    7. public class LivesManager : MonoBehaviour
    8. {
    9.     public int defaultLives;
    10.     public int livesCounter;
    11.  
    12.     public Text livesText;
    13.  
    14.     private GameManager theGM;
    15.  
    16.  
    17.     // Start is called before the first frame update
    18.     void Start()
    19.     {
    20.         livesCounter = defaultLives;
    21.         theGM = FindObjectOfType<GameManager>();
    22.     }
    23.  
    24.     // Update is called once per frame
    25.     void Update()
    26.     {
    27.         livesText.text = "x " + livesCounter;
    28.  
    29.         if (livesCounter <=0)
    30.         {
    31.             theGM.Lose();
    32.         }
    33.     }
    34.  
    35.     public void RemoveLives()
    36.     {
    37.         livesCounter--;
    38.     }
    39. }
    40.  
    This is the part of the movement script that references the lives
    Code (CSharp):
    1.  private void OnTriggerEnter2D(Collider2D other)
    2.     {
    3.         if (other.gameObject.tag == "enemy")
    4.         {
    5.             theLM.RemoveLives();
    6.         }
    7.  
    8.         else if (other.gameObject.tag == "spike")
    9.         {
    10.  
    11.             theLM.RemoveLives();
    12.         }
    13.        
    14.            
    15.     }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,520
    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.

    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.
     
  3. robhertino

    robhertino

    Joined:
    Feb 2, 2021
    Posts:
    4
    Thanks for you reply I seen your reply in another post with this error and I still haven't a clue why it is a null, I have only started using Unity recently and cannot understand why this error is appearing hence why I made a post.
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,520
    Have you isolated the actual object that is actually null?

    This step is not optional unfortunately, and only you are running your project.

    Remember it could be code, it could be setup, doesn't matter.

    You still MUST IDENTIFY WHAT IS NULL.
     
  5. robhertino

    robhertino

    Joined:
    Feb 2, 2021
    Posts:
    4
    Yes I have the object that is causing the Null is the main character when he hits an enemy, instead of trigerring the RemoveLives method it throws an error for having a Null.
    I'm losing my mfking hair trying to figure this out please help me sir.
     
  6. robhertino

    robhertino

    Joined:
    Feb 2, 2021
    Posts:
    4
    Nevermind I'm an idiot
     
    Kurt-Dekker likes this.
  7. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,520
    No, no, I do it all the time... see my avatar? If you have a dog or other small animal (or even a house plant), bring it over to your keyboard and explain step by step what you're doing. As you do so you will suddenly see your error and save a lot of time.

    Don't forget to give your dog / pet a treat for their kind help. Or maybe give your houseplant a bit of water... that is, if it's not a low-water cactus or something.
     
    robhertino likes this.