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

What is a NullRefrenceException and how do I fix it?

Discussion in 'Editor & General Support' started by nuclearsquiddy, Mar 6, 2020.

  1. nuclearsquiddy

    nuclearsquiddy

    Joined:
    Feb 11, 2020
    Posts:
    17
    So I'm trying to create a health bar for my game, and I have 2 scripts I'm referencing to get it to work. The one managing the UI is tied to the bar itself
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class HealthBar : MonoBehaviour
    7. {
    8.    public Slider slider;
    9.  
    10.    public void SetMaxHealth(int health)
    11.    {
    12.        slider.maxValue = health;
    13.        slider.value = health;
    14.    }
    15.  
    16.    public void SetHealth(int health)
    17.    {
    18.        slider.value = health;
    19.    }
    20. }
    21.  
    and the one managing the health is tied to the player
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerStats : MonoBehaviour
    6. {
    7.       public int maxHealth = 100;
    8.       public int currentHealth;
    9.       public HealthBar healthBar;
    10.     void Start()
    11.     {
    12.        currentHealth = maxHealth;
    13.        healthBar.SetMaxHealth(maxHealth);
    14.     }
    15.  
    16.     void Update()
    17.     {
    18.        if (Input.GetKeyDown(KeyCode.Space))
    19.        {
    20.          TakeDamage(20);
    21.        }
    22.     }
    23.     void TakeDamage(int damage)
    24.     {
    25.         currentHealth -= damage;
    26.         healthBar.SetHealth(currentHealth);
    27.     }
    28. }
    Basically, I'm testing to see the health bar in action by removing hitpoints with the space key. Problem is that nothing is happening to the UI, my player is taking damage, and the debug console is flooded with said errors. Everything but the slider is working fine. Help would be much appreciated, especially a solution.
     
  2. Carwashh

    Carwashh

    Joined:
    Jul 28, 2012
    Posts:
    746
    nullreference means you're trying to use a property that doesn't reference an object. I assume you haven't assigned healthBar in the inspector and all the null references are from where you use them.
     
  3. nuclearsquiddy

    nuclearsquiddy

    Joined:
    Feb 11, 2020
    Posts:
    17
    Could you explain? I'm not sure how to do that. As far as I can tell my script is active and assigned to the health bar, or are you saying I should drag it to the character?
     
  4. nuclearsquiddy

    nuclearsquiddy

    Joined:
    Feb 11, 2020
    Posts:
    17
    My brain does not like to function sometimes. Thank you for your help, I've solved it lol.