Death Counter not working

I’m adding a Death Counter to my game, so every time I die, it goes up by 1. For that, I created a deaths int variable, and made it so that everytime it enters a trigger tagged with “hazard” (everything that kills my character) it goes up by one

private void OnTriggerEnter2D(Collider2D col)
    {       
        //Death

        if (col.gameObject.tag == "Hazards")
        {
            //Code that locks my player's control, plays a dead animation, resets the position, etc.
            deaths ++;
        }
    }

For now, I’m trying with a Debug.Log that shows my number of deaths, but the thing is that the number is not going up consistently. Sometimes when I die and check, it goes up by one death, but sometimes it goes up up to three deaths in a single one

(I only died once in intervals like 10 to 13 deaths)

I also tried changing deaths++; to deaths += 1; but the result is still the same. It’s in the Trigger Enter method, so I suppose it theoretically should only happen when firstly touching the collision, why does it repeat? Because other codes like the particles and the sound effect doesn’t play multiple times, only the variable goes up multiple times

You’re probably having the object collide in multiple frames/times which each collision. Try adding a debug message in the OnCollide function to debug.

Maybe entering multiple different ones at once? A quick fix would be to just add a timer or boolean to death incrementing then count down or flip false when respawned.

1 Like

That worked, I tried a Debug.Log and for some reason the collision is detected multiple times, so I set a bool to be true and added the death and turned it false while respawning, now it goes up by one only, thank you!