Singleton Alternative
using UnityEngine;
public class UIManager : MonoBehaviour
{
private static UIManager _instance;
private void Awake()
{
if (_instance == null)
{
_instance = this;
DontDestroyOnLoad(gameObject);
}
else if (_instance != this)
{
Destroy(gameObject);
}
}
public void UpdateHealthDisplay()
{
// ...
}
}
public class Player : MonoBehaviour
{
private int health;
public void TakeDamage(int damage)
{
health -= damage;
UIManager.Instance.UpdateHealthDisplay(health);
}
}Last updated