Mosswine
Mosswine
CC#
Created by Mosswine on 2/1/2023 in #help
✅ 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; } 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; } }
11 replies