Help With OnTriggerEnter (Collider)


Below is a function, and whatever steps into the collider is added to a GenericList. This means that whatever steps into the collider is added to the list. Is there a way to only add objects to the list with a specific tag, eg: "Enemy"?

The way I'm doing it does not seem to work, if you could help me then that would be great.

function OnTriggerEnter (enemy : Collider) 

Here is the var for the List:

var closeObjects = new System.Collections.Generic.List.<Transform>();

FindWithTag() returns a game object in the scene (if there are any) with the specified tag. If I'm not mistaken, your code basically equates to the following:

if there are any game objects in the scene with the tag 'Enemy'
    add enemy.transform to the list

As you can see, the conditional doesn't (necessarily) have anything to do with the game object associated with 'enemy'. If you want to determine specifically whether the object associated with 'enemy' has a particular tag, you should check the value of enemy.gameObject.tag rather than using FindWithTag().