OnTriggerEnter2D(Collider2D) firing twice, once for each game object

I have two player game objects. One for player one, and one for player two. I’m trying to make it so when one player jumps on top of the other, the other loses a life. How I’m trying to accomplish this is by having two colliders on each game object. One box collider 2D and one edge collider 2D. The box collider is a regular collider that surrounds the sprite, and the edge collider is a trigger collider that covers the top edge of the box collider.

Attached to the game objects is a script that contains the following method:

private void OnTriggerEnter2D(Collider2D other)
{
    Debug.Log(transform.name + " " + other.name);
}

When I play the game, and a player jumps on top of the other, the following message is printed to the console:

It is as if the OnTriggerEnter2D(Collider2D) method fires for both objects, even though only one is entering the others trigger collider. Right? The documentation for MonoBehavior.OnTriggerEnter2D(Collider2D) reads:

which I interpret that it should only fire for the game object that is being jumped on. What am I misunderstanding?

Are you sure you don’t have a script on each object?
It’s giving it’s transform.name as two different objects. Also, renaming a clone would be a good idea, but one transform is the clone and one transform is the player. The script would have to be on two different objects to list two different transform names.

The script is on both objects, but the way I see it, only one of the game object’s trigger collider is actually touched, so shouldn’t it only be fire once?

That is odd, but for some reason both triggers are firing. You’ll just have to do further testing to find out why.

It’s normal. They’re both “touched” as one runs into the other. In your example would the player losing health always have a lower y position ? (the other person had to jump on top?) :slight_smile:

Pretty sure you could also check the type of collider on collision to determine who’s who, but if your tests work reliably with a greater y position, that seems simple, too.