::Here is my code
Health controller
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class HealthController : MonoBehaviour
{
PlayerController player;
private GameObject[] heartContainers;
private Image[] heartFills;
public Transform heartsParent;
public GameObject heartContainerPrefab;
// Start is called before the first frame update
void Start()
{
heartContainers = new GameObject[PlayerController.Instance.MaxHealth];
heartFills = new Image[PlayerController.Instance.MaxHealth];
PlayerController.Instance.onHealthChangedCallback += UpdateHeartsHUD;
InstantiateHeartContainers();
UpdateHeartsHUD();
}
// Update is called once per frame
void Update()
{
}
void SetHeartContainers()
{
for(int i = 0; i < heartContainers.Length; i++)
{
if(i < PlayerController.Instance.MaxHealth)
{
heartContainers[i].SetActive(true);
}
else
{
heartContainers[i].SetActive(false);
}
}
}
void SetFilledHearts()
{
for (int i = 0; i < heartFills.Length; i++)
{
if (i < PlayerController.Instance.Health)
{
heartFills[i].fillAmount = 1;
}
else
{
heartFills[i].fillAmount = 0;
}
}
}
void InstantiateHeartContainers()
{
for(int i = 0; i < PlayerController.Instance.MaxHealth; i++)
{
GameObject temp = Instantiate(heartContainerPrefab);
temp.transform.SetParent(heartsParent, false);
heartContainers[i] = temp;
heartFills[i] = temp.transform.Find(“HeartFill”).GetComponent<Image>();
}
}
void UpdateHeartsHUD()
{
SetHeartContainers();
SetFilledHearts();
}
}
Relevant Player Controller Code
[Header(“Health Settings”)]
public int Health;
public int MaxHealth;
public delegate void OnHealthChangedDelegate();
[HideInInspector] public OnHealthChangedDelegate onHealthChangedCallback;
[Space(5)]