Is this normal behavior for edge collider triggers (2D)?

I’m trying to create a tower defense game, and I want to prevent the player from placing towers on the path. I tried using colliders (set as triggers) to accomplish this, but I’m running into a problem where the OnTriggerEnter2D and OnTriggerExit2D calls don’t match up. OnTriggerEnter2D is called more often than OnTriggerExit2D, which makes it impossible to keep track of the number of invalid objects the tower is touching.

I’ve created a new project to isolate the behavior… This is what I have:

There’s two game objects.
Both have sprite renderer components.
Both have rigidbody 2D components.
The circle has a circle collider 2D component.
The path has an edge collider 2D component.
Both colliders are triggers.
Neither rigidbody components are kinematic.

The circle game object has the following script:

using UnityEngine;
using System.Collections;

public class CircleScript : MonoBehaviour {

	private int _collisionCount = 0;

	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () 
	{
		Vector3 newPosition = Camera.main.ScreenToWorldPoint (Input.mousePosition);

		newPosition.z = 0;
		transform.position = newPosition;
	}

	void OnTriggerEnter2D(Collider2D collider)
	{
		_collisionCount++;
		Debug.Log ("on enter. count: " + _collisionCount);
	}

	void OnTriggerExit2D(Collider2D collider)
	{
		_collisionCount--;
		Debug.Log ("on exit. count: " + _collisionCount);
	}
}

The script moves the circle to the mouse position, and handles calls to OnTriggerEnter2D and OnTriggerExit2D.

The behavior I was hoping for was for the trigger Enter/Exit calls to match up so that _collisionCount would be zero when the circle isn’t touching the edge collider. But for some reason, they don’t match up, and _collisionCount ends up being greater than zero because OnTriggerEnter2D is called more frequently.

If I edit the edge collider so that it only has 2 vertices, then I get the behavior I expect. It seems like each line segment making up the edge collider is calling OnTriggerEnter2D correctly, but randomly missing some calls to OnTriggerExit2D.

EDIT: Now that I think about it, it seems that OnTriggerExit2D is being called the correct number of times (once for the entire edge collider), and OnTriggerEnter2D is being called too many times (once for each line segment of the edge collider).

Is this a bug, or am I just doing something wrong? I’ll attach the project to this post.

Thanks.

[20682-trigger+bug.zip|20682]

Hello,

The reason OnTriggerEnter is called more than once, is because it represents each collision found. An object might collide with another one multiple times, while still being inside of it. OnTriggerExit is only called when the object is entirely out of the trigger object. If you want to keep track of which trigger objects you are inside of, you need to keep a collection that doesn’t have any duplicate items, such as a dictionary where the key is the trigger object and the value could be a boolean, or you can just manage that yourself by checking if the object is already in the collection before adding it into it.

Hope this helps,
Benproductions1