✅ 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;
}
}
5 Replies
Show error
Full
Something somewhere is
null
Avoid calling GetComponent for the same component and object twice
According to your log, the error is at line 20 which is where you use GetComponent. Make sure that said component exists on your gameobject!
If it does not exist it will return null and give you the error you are getting right now.
I tried only calling the get component without .currenthealth or .maxhealth but then for some reason it won't let me use those fields as variables. There's a character limit in the original post and I was in kind of a rush to post it before I had to leave for work 😅
I'm specifically doing it apparently the hard way because I want a generic solution i can use for more than just healthbars. As I posted in the statblock, i have several stats I want visualized
Further detail now that I have a second to breathe:
The character game object does have the statblock attached to it. Ive been able to call the movespeed float in my movement script, so I know Unity knows its there and can access it.
Its just in this instance, with the way Im trying to set up my slider, that it isnt working.
And the error isnt a compile error, its just unity throwing an exception in the console about the getcomponent as Rust pointed out
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.