Why is my player dying multiple deaths?

I cant seem to figure out what the problem is, shown in this gameplay video:

athleticdefiantkob

The player has a health of 3, and is decremented every time player is hit, once it reaches 0 a life is lost (shown by the 3 medals at top right)

The bullet frenzy of the boss in the beginning of the video shows the problem. Player is hit by many bullets, but instead of losing one life after taking 3 hits, he somehow loses 2 lives. As if player keeps taking damage even before next life is instantiated. Its worth mentioning that the next life wont start until I press the fire key, so it doesnt self respawn.

Later in video I demonstrate that the 1 hit system is also working, when player is hit by 1 bullet 1/3 of his life drops. But I cant seem to figure out why multiple lives are lost when many bullets hit.

Below is the OnTriggerEnter2d code on the tank bullet:

private void OnTriggerEnter2D(Collider2D collision)
    {
        if (collision.CompareTag("Player"))
        {
            Shooter shooter_ = collision.GetComponent<Shooter>();
            shooter_.TakeDamage(dmg);
            GameObject effect_ = Instantiate(bulletEffect, transform.position, transform.rotation);
            Destroy(effect_, 0.5f);
            Destroy(gameObject);
        }

    }

And this is the TakeDamage function on the player:

public void TakeDamage(int damage)
        {
            health -= damage;     
            if (health <= 0)
                LevelManager.Instance.KillCharacter(this);
        }

Show your KillCharacter code

In your code, there is nothing between a “dead” player and a “live” player. The corpse can take damage and be killed again.

What you want to do is to check if the health is zero or less BEFORE you do anything that cannot be done to a corpse. Usually this is done with some kind of flag that you set when you first detect that the character is dying. Then you can check that flag wherever necessary.

public bool isAlive = true;

public void TakeDamage(int damage)
        {
            if (!isAlive)
                return;
            health -= damage;  
            if (health <= 0)
            {
                isAlive = false;
                LevelManager.Instance.KillCharacter(this);
            }
        }
2 Likes

Thats very helpful. Many thanks :slight_smile:

Didn’t know that this was an actual problem that a corpse can take damage even after the gameObject has been destroyed lol

Well, your code can have any problem you write into the code :stuck_out_tongue:

2 Likes

Just to be clear, a Destroy(gameObject) call really just marking the object for destruction in the future. It is like saying “at the end of this game frame, destroy the object.” If you have many bits of code interacting with a gameObject, lots of things can happen in the remaining time before the end of the game frame.

It’s not clear from your code whether you’re destroying the victim who died though. Seems more like you’re destroying the bullet. Hard to tell without more context.

1 Like

You should familiarize yourself with Finite-state machine - Wikipedia