- Home /
Collision Tags
So I have the following code:
function OnTriggerEnter (other : Collider)
{
if (other.gameObject.tag=="GREEN")
{
greenGemCount+=1;
}
}
but my gameObject doesn't seem to register the collision, even with the "Green" gem having the "GREEN" Tag, however when I comment out the
if (other.gameObject.tag=="GREEN")
it seems to work, yet every "Gem" touched increases the value of greenGemCount. It appears correct? Any help, would be apprecited!
--WHYME
Have you tried doing a Debug.Log on the other.gameObject?
Are you sure you have a "GREEN" tag and not accidentally a "GREEN" layer. I've done that so many times before.
Answer by Doireth · Feb 02, 2013 at 09:51 AM
It's generally advisable to use CompareTag() (see here). For example:
if(other.gameObject.CompareTag("GREEN"))
{
greenGemCount++;
}
I have found from my own experience that CompareTag is the most reliable way. Also, I'm fairly sure that tags are case sensitve.
It is reliable to compare == "GREEN" but CompareTag is faster. Like you say, they are case sensitive
Thank You both, it appears I managed to tag the parent of "GREEN", which has no collider, ins$$anonymous$$d of the child which does, how frustrating..
Your answer
![](https://koobas.hobune.stream/wayback/20220613092020im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Display variables in GUIText? 1 Answer
var TheCollision : Collision 1 Answer
How do you change a variable in a script, that is on an object instantiated from a prefab? 0 Answers
Error(non static member): using public variable from other script JS 1 Answer
Adding varibales from other scripts 3 Answers