I am having an issue with a health system I am making for my game. I have been at it for 3 days now and have not made much progress. For reference, I am following this tutorial by code monkey: Code Monkey Health Tutorial. The goal is to have two buttons, one for healing and one to damage the player, and a Text Mesh Pro UI Text to show the health of the player, just for testing purposes. Unlike the tutorial, the buttons and text are just on a canvas, not attached to the player, as I am setting it up as a form of a HUD system. also, forgive the formatting, during troubleshooting, I was more focused on trying to fix my issue and my code became a bit of a mess. Sorry for that.
I have three scripts
using UnityEngine;
public class HealthSystem
{
private int _health;
private int _healthMax;
public HealthSystem(int healthMax)
{
this._healthMax = healthMax;
_health = healthMax;
Debug.Log("healthSystem Created");
}
public int GetHealth()
{
return _health;
}
public void Damage(int damageAmount)
{
_health -= damageAmount;
if (_health < 0) _health = 0;
}
public void Heal(int healAmount)
{
_health += healAmount;
if (_health > _healthMax) _health = _healthMax;
}
}
using UnityEngine;
public class GameHandler : MonoBehaviour
{
private HealthButtons _healthButtons;
private void Start()
{
HealthSystem _healthSystem = new HealthSystem(100);
Debug.Log("Health = "+_healthSystem.GetHealth());
_healthButtons.Setup(_healthSystem);
}
}
}
using UnityEngine;
using TMPro;
public class HealthButtons : MonoBehaviour
{
[SerializeField]
private TextMeshProUGUI numberText;
private static HealthSystem _healthSystem;
private int intToStringHealth;
public void DamageButton()
{
_healthSystem.Damage(10);
Debug.Log(_healthSystem.GetHealth());
}
public void HealButton()
{
_healthSystem.Heal(10);
Debug.Log(_healthSystem.GetHealth());
}
public void Setup(HealthSystem healthSystem)
{
if (healthSystem == null) Debug.Log("healthSystem");
Debug.Log(healthSystem);
_healthSystem = healthSystem;
Debug.Log("Setup is running");
}
private void Update()
{
intToStringHealth = _healthSystem.GetHealth();
numberText.text = intToStringHealth.ToString();
}
}
my issue currently is Im trying to create an instance of HealthSystem
in GameHandler
, which I am successful in, then pass the information to HealthButtons
using _healthButtons.Setup()
. This is where I become unsuccessful as I am getting a NullReferenceException error.
from my efforts of troubleshooting currently, I have determined I am not calling the instance correctly, but I do not want to use code monkey's libraries. I have tried making all classes monoclasses and attaching them to game objects and using SerializeFields for all my variables, which does work, but I am not sure that is best practice as I do know linking everything up in the inspector can be a chore if something goes wrong.
This tells me the solution should be fairly simple, I am just not able to articulate the question into google to get the correct answer. It may be a bit overkill just to test a function, but I would like to understand what I am doing wrong and get better. Thank you very much for your help.