# Throttling or adding delays between OnTriggerEnter2D calls

I have 2 game objects: Explosion and Enemy. At any given point, an enemy can collide with multiple Explosion game objects at the same exact time. This causes a few issues when I'm trying to destroy the Enemy game object.

This is the function that destroys the game object:

bool isDead = false;

public void KillCharacter () {
Debug.Log ("kill enemy...");
}
}


If I use OnTriggerEnter2D inside the Enemy script, like so:

void OnTriggerEnter2D (Collider2D other) {
if (other.gameObject.tag == "Explosion") {
KillCharacter();
}
}


In case two explosions spawn on the position of the enemy, Debug.Log inside the KillCharacter function will fire twice, even though I set the isDead bool to true on the first run.

The only solution I found was to call KillCharacter from the Explosion script, like so:

void OnTriggerEnter2D (Collider2D other) {
if (other.gameObject.tag == "Enemy") {
other.gameObject.GetComponent<Enemy> ().KillCharacter ();
}
}


Is there a way to run the OnTriggerEnter2D from the Enemy script, as I originally intended? Or is having the KillCharacter handle all the death logic and calling it from outside good enough?

• The behavior you describe shouldn't really happen -- you set isDead to true the first time, so even if two collisions are happening at a nearly identical time, they must still be handled sequentially in some way, and the very first one closes the processing for the second. Could you check your code some more to see if either the isDead is perhaps set to false somewhere again -- or that maybe your script is attached to two different gameObjects, and you are mistaking the "kill enemy" debug log for a single gameObject? E.g. give all objects a different name and use print("killed " + name). Feb 11, 2020 at 21:39
• please share full scriptd Feb 12, 2020 at 8:00

Tested your question and code out as at first I thought it was regarding the 'Is Trigger' option on the collider. However, it's working as intended.

As shown here: https://imgur.com/a/oPJMFTl.

The script is on the Enemy tagged object. Tested with the 'Is Trigger' checked on one and both of the Objects "Enemy", "Explosion".

The only thing I changed from your code was set the bool isDead to public so that it shows on the inspector.

Most likely problem is that your bool isDead is being altered elsewhere.

• Forgot to mention that I'm using Unity v2019.3.0f6. Not sure if it makes a difference, but the explosions spawn on the location of the enemy at the same time, not the other way around. I tried logging inside OnTriggerEnter2D and KillCharacter and it shows like this: i.imgur.com/DsBnJy4.png - isDead is set to true before the first killed char log is output. This is really weird. Feb 12, 2020 at 9:14
• using same version When you spawn(instantiate) the explosions they are happening sequentially through each instantiation line and not at the exact same time, so it shouldn't be possible for a double trigger. I recommend that you debug by creating new explosion and enemy objects with just the code above attached to the enemy object and see if that works as you intended.
– RBee
Feb 12, 2020 at 21:38