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:
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);
}
}
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.