Yes true, but the unit is also dead when health == 0
so you’d need to make it health <= 0
Also, at a later stage you’ll add another event (OnDamage
) that need to always run when damage is taken, but it needs to run after health < 0
(because we don’t want to notify listeners that damage was taken and present a health value that’s less than 0 so we want to set the value to 0 first), but before health == 0
(because we want listeners to respond to the damage before listeners that respond to the death). So, rather than
health -= damageAmount;
if (health <= 0)
{
health = 0;
OnDamage.Invoke();
Die();
}
else
{
OnDamage.Invoke();
}
we go with just splitting the health check in 2
health -= damageAmount;
if (health < 0)
{
health = 0;
}
OnDamage.Invoke();
if (health == 0)
{
Die();
}
You are welcome to change it if you want (I have exactly 0 projects that match the lecture code), just keep the above in mind.