How to solve error "object reference not set to an instance of an object"

I’m new to programming and I’m currently working on the 2D Beginner Adventure Game project from Unity Learn. Here’s the link for it.

Now I’m on “Display character health on the UI” step 7, and when I try to run my code it shows the following error:

NullReferenceException: Object reference not set to an instance of an object
PlayerController.ChangeHealth (System.Int32 amount) (at Assets/Scripts/PlayerController.cs:65)
DamageZones.OnTriggerStay2D (UnityEngine.Collider2D other) (at Assets/Scripts/DamageZones.cs:13)

I tried searching it up but I don’t really understand because of the difficult terminology, so I’m wondering if someone can explain how to solve it in simple terms.

Here are my codes for the player character:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;

public class PlayerController : MonoBehaviour
{
    public InputAction MoveAction;
    Rigidbody2D rigidbody2d;
    Vector2 move;
    public float speed = 5.0f;
    
    public int maxHealth = 5;
    public int currentHealth { get; private set; }

    public float timeInvincible = 1.0f;
    bool isInvincible;
    float damageCooldown;

    void Start()
  {
     MoveAction.Enable();
     rigidbody2d = GetComponent<Rigidbody2D>();

     currentHealth = maxHealth;
  }
 
  // Update is called once per frame
  void Update()
  {
     move = MoveAction.ReadValue<Vector2>();

     if (isInvincible)
       {
           damageCooldown -= Time.deltaTime;
           if (damageCooldown < 0)
               isInvincible = false;
       }
   }

// FixedUpdate has the same call rate as the physics system
  void FixedUpdate()
  {
     Vector2 position = (Vector2)rigidbody2d.position + move * speed * Time.deltaTime;
     rigidbody2d.MovePosition(position);
  }

  public void ChangeHealth (int amount)
  {
     if (amount < 0)
       {
           if (isInvincible)
               return;          
           isInvincible = true;
           damageCooldown = timeInvincible;
       }
     currentHealth = Mathf.Clamp(currentHealth + amount, 0, maxHealth);
     UIHandler.instance.SetHealthValue(currentHealth / (float)maxHealth);
  }
}

and for the damage zone:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class DamageZones : MonoBehaviour
{
void OnTriggerStay2D(Collider2D other)
   {
       PlayerController controller = other.GetComponent<PlayerController>();

       if (controller != null)
       {
           controller.ChangeHealth(-1);
       }
   }
}

I fixed it. I found out that the name before instance is supposed to be the name of the script, and I didn’t use the same script name as the tutorial. Changed it and worked.

Cool thanks for coming back… this error is really common and has an infinite possible set of root causes.

But it only has ONE fix process… The answer is always the same… ALWAYS!

How to fix a NullReferenceException error

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

NullReference is the single most common error while programming. Fixing it is always the same.

Some notes on how to fix a NullReferenceException error in Unity3D:

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

1 Like