- Home /
Help With OnTriggerEnter (Collider)
Hello,
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)
{
if(GameObject.FindWithTag("Enemy")){
closeObjects.Add(enemy.transform);
}
}
Here is the var for the List:
var closeObjects = new System.Collections.Generic.List.<Transform>();
Answer by Jesse Anders · Nov 28, 2010 at 03:35 PM
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().
Well, I have a turret that tracks anything that goes within its radius. And I only want to track 'Enemy'. So what do you mean check the value? Could you give an example please? Thanks
Wait, I did this: if(enemy.gameObject.tag == "Enemy"){ - And it works fine! Thanks Jesse!
Your answer
Follow this Question
Related Questions
Why isn't my tag working (on trigger enter) 2 Answers
OnTriggerEnter not working as intended 1 Answer
Change tags with col.gameObject.tag inside an OnTriggerEnter function NOT WORKING 1 Answer
difference between GameObject.FindGameObjectWithTag(Tag tag) and GameObject.FindWithTag(Tag tag) 3 Answers
Homing Missile target with "Find Objects With Tag" not working? 2 Answers