OnTriggerEnter2D is being executed multiple times,OnTriggerEnter2D

Hello,

My code has been working fine, but my project got sidetracked for about a year.
I’m now working with the latest Unity version and I noticed my OnTriggerEnter2D is being executed multiple times. Not just in 1 part of my code but in multiple places. This has the effect that my player respawns 1 to 4 times instead of the 1 instance I want. Also picking up ammo sometimes gives me up to 4 times the amount of ammo it should.

Anyone know what could trigger this issue?

This is a part that has this issue

 void OnTriggerEnter2D(Collider2D col)
    {
        Vector3 colPos = col.gameObject.transform.position;
        Quaternion colRot = col.gameObject.transform.rotation;

        // get reference to sprite renderer
        SpriteRenderer sr = this.transform.gameObject.GetComponent<SpriteRenderer>();

        //It can destroy players
        if (col.gameObject.tag.Contains("Player"))
        {
            if (gameObject.name.Contains("Lava"))
            {
                soundEffect("FallInLava");
            }
            if (gameObject.name.Contains("Explosion"))
            {
                //Debug.Log("Mine Hit: " + gameObject.name + " by " + col.gameObject.name);
                col.gameObject.GetComponent<ShotPlayer>().Hurt(col.gameObject.tag, playerNum, false);
            }
            else
            { col.gameObject.GetComponent<ShotPlayer>().Hurt(col.gameObject.tag, 0, true); }

            if (setNewSprite && (sr.sprite != newSprite)) { sr.sprite = newSprite; }
        }
        //It can destroy pickups
        else if (col.gameObject.name.Contains("Pickup"))
        {
            if (gameObject.name.Contains("Lava")) { soundEffect("FallInLava"); }
            Destroy(col.gameObject, 0);
            //If it's a mine  we want to see explosions!
            if (col.gameObject.name.Contains("Mine"))
            {
                GameObject explosion = Instantiate(explosionPrefab, colPos, colRot) as GameObject;

                soundEffect("Explosion");
                explosion.GetComponentInChildren<HostileTriggerDetection>().playerNum = 0;
                Destroy(explosion.gameObject, 2f);
            }
            else
            {
                GameObject pickupAnimtion = Instantiate(pickupPrefab, colPos, colRot) as GameObject;
                soundEffect("destroy pickup");
                Destroy(pickupAnimtion.gameObject, 2f);
            }
        }
        //It can destroy Destroyable Obstacles
        else if (col.gameObject.tag.Contains("DestroyableObstacle"))
        {
            if (gameObject.name.Contains("Lava")) { soundEffect("FallInLava"); }
            col.gameObject.GetComponent<OnDestroyAnimation>().DestroyIt(gameObject.name);
        }
    }

Thanks for the help guys!

@nuubje006
Try using an if-condition on line 40 to check for the ammunition instead of else condition.
if that doesn’t work. use boolean to instantiate your prefab only once.
This post might help.

I had a similar problem in a very different way. Rockets hitting a car sometimes doing 4x damage. I wrote it off as a ‘critical hit’ feature for a while ;).

I solved it by adding a small cooldown for the event (very small was all I needed) and a boolean, something like ‘hasCollided’ set to true when it happened, and false when the cooldown was hit.

Something so simple might not work for your case, and it looks like you have quite a few possible triggers going on, so who knows. I could recommend some roundabout tips: using a script like SampleCollider, attaching to collider, onTriggerEnter running

SampleCollider sampleCollider = other.GetComponent<SampleCollider>();.

Then, in that SampleCollider class, you can create some unique string or group identifier (manually set for static objects or dynamically overridden, pretty easy to do). And then it’s just a matter of adding a previousSampleCollider string, and check it against the sampleCollider.stringID that you’ve created in that class, and afterwards set previousSampleCollider equal to that same string for the next pass.

I could see this still having a few issues (multiple types and all), but none of them unsolve-able.