I have a game object with only a box collider 2d (IsTrigger is ON) attached to it. I use this to detect ( using OnTriggerEnter2D() ) when player falls on it so that the life count is reduced by 1. But most of the times, it is called twice and life count decrements by 2. After a lot of debugging I found out that this issue only happens when IsTrigger is ON. It doesn’t happen when IsTrigger is OFF. Why is it happening? Is there a problem in my code or is it some sort of unity error?
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.gameObject.tag == "Deadly")
{
lifeCount = lifeCount - 1;
player.SetActive(false);
if (lifeCount <= 0)
{
DiedPanel.SetActive(true);
}
else
{
//Some code here to reposition player
//It is made sure that the player is not positioned on the Deadly.
player.SetActive(true);
}
}
}