'Trigger gets detected twice but not everytime

public int health = 100;

public int damage = 20;

void OnTriggerEnter2D(Collision2D collision)
{
    if (collision.gameObject.tag == "Bullet")
    {
        health -= damage;
        Debug.Log("trafiles");
    }
}
void FixedUpdate()
{
    if (health <= 0)
    {
        Destroy(gameObject);
    }
}

I'm making a 2d topdown shooter and this is script responsible for enemies taking damage and dying. The thing is, sometimes trigger gets detected twice and bullet does double damage, it doesn't happen everytime tho.



Solution 1:[1]

It would be helpful to log the interval between the two Trigger.

Sometimes a second Trigger occurs before the first Trigger is handled.
(in the same object)
If there is a log you can check this.

Debug.Log(string.Format("[{0}] Call", DateTime.Now.Ticks.ToString()));

If it occurs in another object, it is a normal operation,
If it is occurring in the same object, it is possible to bypass it by storing the last occurred object in a separate variable.

for example
Collision2D temp;
void OnTriggerEnter2D(Collision2D collision)
{
    if (collision != temp)
    {
        temp = collision;
        health -= damage;
        Debug.Log("trafiles");
    }
}

This is one way, not the right answer.
You need to find out what caused the "Trigger" to occur twice.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1