Skip to content

Instantly share code, notes, and snippets.

@unity3dcollege
Created December 29, 2016 06:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save unity3dcollege/bf4a2dde3daad6c0e2706555ffaa5dca to your computer and use it in GitHub Desktop.
Save unity3dcollege/bf4a2dde3daad6c0e2706555ffaa5dca to your computer and use it in GitHub Desktop.
using System;
using UnityEngine;
public class Health : MonoBehaviour
{
[SerializeField] private int startingHealth = 100;
private int currentHealth;
public event Action<float> OnHPPctChanged = delegate { };
private void Start()
{
currentHealth = startingHealth;
}
public float CurrentHpPct
{
get { return (float) currentHealth / (float) startingHealth; }
}
public void TakeDamage(int amount)
{
if (amount <= 0)
throw new ArgumentOutOfRangeException("Invalid Damage amount specified: " + amount);
currentHealth -= amount;
OnHPPctChanged(CurrentHpPct);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment