© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•4y ago•
10 replies
Mosswine

✅ Object reference error? Using Unity

I'm trying to create a slider system to visualize various status bars using references instead of directly setting values in each bar. I can't seem to wrap my head around why this is throwing an 'Object reference not set to an instance of an object' issue.

My statblock is here:


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

public class FinkratStatblock : MonoBehaviour
{

    public float MaxHealth = 100;
    public float CurrentHealth = 100;

    public float MaxEnergy = 200;
    public float CurrentEnergy = 200;

    public float MoveSpeed = 7.0f;

    public float MaxHunger = 100;
    public float CurrentHunger = 0;

    public float MaxThirst = 100;
    public float CurrentThirst = 0;

    public float MaxSleepy = 500;
    public float CurrentSleepy = 0;

    public float MaxCatalystOil = 200;
    public float CurrentCatalystOil = 200;

    public float AfraidAmount = 0;

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

public class FinkratStatblock : MonoBehaviour
{

    public float MaxHealth = 100;
    public float CurrentHealth = 100;

    public float MaxEnergy = 200;
    public float CurrentEnergy = 200;

    public float MoveSpeed = 7.0f;

    public float MaxHunger = 100;
    public float CurrentHunger = 0;

    public float MaxThirst = 100;
    public float CurrentThirst = 0;

    public float MaxSleepy = 500;
    public float CurrentSleepy = 0;

    public float MaxCatalystOil = 200;
    public float CurrentCatalystOil = 200;

    public float AfraidAmount = 0;

}


and the healthbar script I'm trying to work on is here. It's saying the error line is line 20, aka the first get component CurrentHealth:



using UnityEngine;
using UnityEngine.UI;

public class Healthbar : MonoBehaviour
{

    public FinkratStatblock finkratStatblock;
  
    public float currentHealth;
    public float maxHealth;
    public Slider slider;
    public Image fillImage;

    private void Awake()
    {
       slider = GetComponent<Slider>();
       currentHealth = GetComponent<FinkratStatblock>().CurrentHealth;
       maxHealth = GetComponent<FinkratStatblock>().MaxHealth;
    }
    // Start is called before the first frame update
    void Start()
    {

        slider.maxValue = maxHealth;
        slider.minValue = 0;
        
        currentHealth = maxHealth;
    }

    void TakeDamage(float amount)
    {
        currentHealth -= amount;
    }

    // Update is called once per frame
    void Update()
    {
        
       slider.value = currentHealth / maxHealth;

    }
}
using UnityEngine;
using UnityEngine.UI;

public class Healthbar : MonoBehaviour
{

    public FinkratStatblock finkratStatblock;
  
    public float currentHealth;
    public float maxHealth;
    public Slider slider;
    public Image fillImage;

    private void Awake()
    {
       slider = GetComponent<Slider>();
       currentHealth = GetComponent<FinkratStatblock>().CurrentHealth;
       maxHealth = GetComponent<FinkratStatblock>().MaxHealth;
    }
    // Start is called before the first frame update
    void Start()
    {

        slider.maxValue = maxHealth;
        slider.minValue = 0;
        
        currentHealth = maxHealth;
    }

    void TakeDamage(float amount)
    {
        currentHealth -= amount;
    }

    // Update is called once per frame
    void Update()
    {
        
       slider.value = currentHealth / maxHealth;

    }
}
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources
Was this page helpful?

Similar Threads

Recent Announcements

Similar Threads

❔ Object reference not working (unity)
C#CC# / help
3y ago
❔ C sharp object reference error
C#CC# / help
4y ago
❔ Unity property unityaction invoke
C#CC# / help
3y ago
✅ cant resolve object reference not set error
C#CC# / help
3y ago