C# - Death on monster in clicker 2D.

Hi guys!

I’m looking for a tip how to make a death script.

I mean when I tap the screen(That will be clickergame) and mob have 0 hp then he die and resp next.

My problem is in code.
Take a look:

if (current_hp_mob < 0) {

            current_hp_mob = constant_hp_mob;
            Click.gold += gold_per_mob;
            int i = Random.Range (0, enemies.Length);
            enemies [i].SetActive (true);
            a = i;


        }
        else
        {
            if ( current_hp_mob < 1 ) {

                enemies [a].SetActive (false);

            }

So when I have ‘tap_dmg’ = 1 then all is right. But when I have tap_dmg = 2 is’nt.

I know because. Because when hp is = 1 so simple math, 1 - 2 = -1, and mob are ‘hiding after death’ when is <1 but when <1 and <0 no.

Have you any tips? I dont want done code, just a simple tip. :slight_smile:

One way you could solve this issue is by doing a calculation for the health and checking against that. What I would do is create a damage method which has a parameter and do something like this:

public void Damage(float amount)
{
    if((currentHealth - amount) < minHealth && isDead)
    {
         currentHealth = minHealth;
          //Spawn loot
         // Instantiate monster
     
    }
}

Something like this should prevent your health from going to minus figures. If you have any more trouble let me know.

Regards

-Joshmond

Thanks joshmond! Thats great tip! :slight_smile: