Multiple Collider detection and OnTriggerEnter calls on the same Collider

When the Player shoots, a ‘missile’ is fired. When this missile explodes (on contact), it spawns an explosion object at its location. This explosion object is picking up multiple OnTriggerEnter2D calls on enemies, which have only one collider on them. As well as this, Physics2D.OverlapCircleAll returns the SAME collider on the enemy multiple times. Here is the necessary code:

Explosion : AttackClass

void Start()
{

    Colliders =
    Physics2D.OverlapCircleAll(gameObject.transform.position,
    0.5f);

    // 0.5f is the radius of the collider

        foreach (var Collider in Colliders)
        {
            if(Collider.tag == "Enemy"){
                print("Hit enemy");
            }
        }
}

public override void OnTriggerEnter2D(Collider2D col){
        if (col.tag == "Enemy"){
            print("Triggered");
        }
}

Most of the time it only prints each statement once, however when the Player is touching the Enemy or sometimes when the Player is below the Enemy and fires a missle, each print statement is printed twice and on some enemies (bigger ones) even three times. There is only one enemy in the scene. I don’t really have any idea how this is happening, but maybe it has something to do with the colliders contact points?

Zombie Enemy:

Slime enemy:

Explosion object:

As you can see my enemies only have one capsule collider on them each, which is triggering OnTriggerEnter multiple times as well as returning multiple times in Physics2D.OverlapCircleAll.

On the slime enemy it would trigger OnTriggerEnter / return multiple times in Physics2D.OverlapCircleAll one time normally and 2 times abnormally (when the Player is positioned extremely close to the enemy, or when the attack comes at a weird angle)

On the zombie enemy it is one time normally and 2-3 times abnormally. I think this is because the zombie enemy has a larger collider?

This issue has been plaguing my development for a while now and i have made a couple of threads on this with no responses.

I hope i have outlined the issue with enough detail and i would very much appreciate any type of response.

Thanks.

Never heard of such a thing. Are you sure that you have only one AttackClass running? Try changing your print to something like:

print(GetInstanceID()+ " is colliding with "+ col.gameObject.GetInstanceID());

Also, you have a variable called Collider, which is also the name of a Unity-defined class. You probably want to change that name.

1 Like

Wow this is it. Two explosions are being spawned however i have no idea why.

public class Karma : AttackClass
{
    public GameObject explosion;
    public float lifeTime;
    void Start()
    {
        lifeTime = 0.5f;
        Invoke("Death", lifeTime);
    }

    void Death(){
        GameObject objectInstance = Instantiate(explosion,
        gameObject.transform.position, Quaternion.Euler(new Vector3(0, 0, 0)));
        print("Spawning explosion");
        Destroy(gameObject);
    }

    override public void OnTriggerEnter2D(Collider2D col){
        Death();
    }
}

This is the code that spawns the explosion. It should only spawn once every time right? I don’t see any situation where it could spawn two explosions. Only one Karma is being spawned as well.

This suggestion helped me troubleshoot, WHAT I THOUGHT, was an OnTriggerEnter registering multiple hits when, on closer inspection of the InstanceID, it was the OnTriggerEnter from the object in the next scene being loaded on top of the player’s current position.

I owe you a beer my good man.