OnTriggerEnter, detecting objects with 2+colliders.

Hello everyone, Im making a kind of tower defense game and have a problem where the enemies in my game have a collider to “collide” (duh) with players and environment and then they have a trigger to know if player is in reach of attack. I’ve set up my turrets with a trigger around them, covering their range of fire. If an enemy enters it “OnTriggerEnter” will add them to a list of targets in range and OnTriggerExit will remove them (when they die, they fall through the floor and exit the trigger before getting destroyed ).

void OnTriggerEnter (Collider col){
		//Debug.Log (col.gameObject.name);
		if (col.gameObject.tag == "Enemigo") {
			targets.Add(col.gameObject);
			Debug.Log (col.gameObject.name + "added");
			Debug.Log (targets.Count);
		}
	}
	
	void OnTriggerExit (Collider col){
		if (col.gameObject.tag == "Enemigo") {
			Debug.Log (col.gameObject.name+"removed");
			targets.Remove(col.gameObject);
			Debug.Log (targets.Count);
		}
	}

The problem is OnTriggerEnter is adding the same object once because of its collider and again because of the “attack range” trigger. Isnt there any way to prevent OnTriggerEnter from detecting other triggers?

Any ideas on how to improve the turret range system is also welcome!

There are a lot of ways you could do this, however:

The “best” way would probably be to never have multiple collider components on the same gameObject, because you it gets messy. Your situation is a good example :). If you have a child gameObject for every collider component you want, you can easily tell the difference. You can probably find out a few ways to do this yourself.