UnityScripterAI
Discord ↗
Link Copied to Clipboard
×
"create a health system"
public class HealthSystem : MonoBehaviour { public int maxHealth = 100; public int currentHealth; public event System.Action OnHealthChanged; private void Start() { currentHealth = maxHealth; } public void TakeDamage(int damageAmount) { currentHealth -= damageAmount; if (currentHealth < 0) { currentHealth = 0; } if (OnHealthChanged != null) { OnHealthChanged(); } } public void Heal(int healAmount) { currentHealth += healAmount; if (currentHealth > maxHealth) { currentHealth = maxHealth; } if (OnHealthChanged != null) { OnHealthChanged(); } } }