Unity health system code example
Example: how to make a simple health system in unity
public int currentHP;
public GameObject Enemy; // These are to find the GameObjects
public GameObject Player;
void Start()
{
(Enemy) = GameObject.Find("Enemy"); // Set this to the GameObjects name
(Player) = GameObject.Find("Player"); // Set this to the GameObjects name
}
void Update()
{
if (currentHP < 1) // This is to destroy the player when you reach 0 HP
{
Destroy(Player); // You can change this to a different method
}
}
private void OnCollisionEnter2D(Collision2D collision) // Detecting the damage
{
if (Enemy)
currentHP =-1;
}
// You can add a health cap if you need to
// (This is the same as 'How to detect damage in unity' I put it on here if someone typed up something different :P)