Source file unity-csharp/ebook/PlayerHealth.cs from the GDnD code examples. Download raw file
// Demonstrates beginner Unity health state for The Architecture of Play ebook.
// Referenced by: docs/ebook/GDnD-Architecture-of-Play.md
using UnityEngine;
public class PlayerHealth : MonoBehaviour
{
[SerializeField] private int maxHealth = 3;
private int currentHealth;
private void Start()
{
currentHealth = maxHealth;
Debug.Log("Player health set to " + currentHealth);
}
public void TakeDamage(int amount)
{
currentHealth -= amount;
if (currentHealth <= 0)
{
currentHealth = 0;
Debug.Log("Player defeated");
}
else
{
Debug.Log("Player health: " + currentHealth);
}
}
}