- Home /
Double-Counted OnTriggerEnter2D Events: Why?
I've got a MeleeHit object created by the hero's attack. It's got a script given below, that applies damage and destroys the attack when it hits an enemy. I found that when the attack hit, the visual effect didn't appear, because the GameObject was instantly being destroyed on collision, so I changed Destroy(gameObject) to Destroy(gameObject,1). No harm, I thought, because the damage only happens on the first frame of collision.
But I'm finding that it happens twice when I do that. One object, two calls to OnTriggerEnter2D, unless the Destroy call happens instantly. Any idea why? The debug messages say "Hit" twice and give frame numbers that are either 1 or 2 apart.
def OnTriggerEnter2D(other as Collider2D):
if other.tag == "Foe":
Debug.Log("Hit: " + other)
Debug.Log(Time.frameCount)
creature = other.gameObject.GetComponent("Creature") as Creature
creature.ChangeHealth(-damage,true)
if death_fx:
Instantiate(death_fx,transform.position,transform.rotation)
Destroy(gameObject,1)
Edit: My other option is to have two separate objects, one with the visual effect and one with the collision logic. That worked, but it's inelegant.
Your answer
![](https://koobas.hobune.stream/wayback/20220613141824im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
OnTriggerEnter2D Doesn't work? ;_; 4 Answers
OnTriggerEnter2D Not Working 1 Answer
Object jumps through roof when holding "Up" button? 1 Answer
How do I make animation transitions a one-way street? 1 Answer
Limiting Physics to X and Y 5 Answers