Health Bar

Hi, I am making a health bar for my game, but when I play, everytime I collide with the wall that has the tag Daño, the game stops and there are two messanges. That first says NullReferenceException: Object reference not set to an instance of an object Health.Start () (at Assets/Scripts/Health.cs:16) and the second says NullReferenceException: Object reference not set to an instance of an object Health.TakeDamage (System.Int32 damage) (at Assets/Scripts/Health.cs:36) Health.OnCollisionEnter (UnityEngine.Collision other) (at Assets/Scripts/Health.cs:29). Can someone please help me? Here is my code.

Script HealthBar:

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

public class HealthBar : MonoBehaviour
{
    public Slider slider;

    public void SetMaxHealth(int health)
    {
        slider.maxValue = health;
        slider.value = health;
    }

    public void SetHealth(int health)
    {
        slider.value = health;
    }
}

Script Health:

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

public class Health : MonoBehaviour
{
    public int maxHealth = 100;
    public int currentHealth;

    public HealthBar healthBar;

    // Start is called before the first frame update
    void Start()
    {
        currentHealth = maxHealth;
        healthBar.SetMaxHealth(maxHealth);
    }

    // Update is called once per frame
    void Update()
    {
      
    }

    private void OnCollisionEnter(Collision other)
    {
        if(other.collider.tag == "Daño")
        {
            TakeDamage(20);
        }
    }

    void TakeDamage(int damage)
    {
        currentHealth -= damage;
        healthBar.SetHealth(currentHealth);
    }
}

Null reference? ALWAYS the same steps… always.

Some notes on how to fix a NullReferenceException error in Unity3D

  • also known as: Unassigned Reference Exception
  • also known as: Missing Reference Exception

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:

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

I think taht null is the SetMaxHealth an the Set Health on the script Health, but I don’t now why.

What mechanism is supposed to set healthBar ?

It is suppost to set de Health Bar with the value of currentHeahlt, and when the game start it is suppost to do the same, but with the maxHealth.

You never initialize the HealthBar you should do this:

    void Start()
    {
        currentHealth = maxHealth;
        healthBar = new HealthBar();
        healthBar.SetMaxHealth(maxHealth);
    }

I made it but the problem is still there.

I alredy found the error, I have the same script to times on the player, but thank you for your help.