Tag just doesn't work in my If statement at all !

Hello eveyrone ! I’m pretty new to Unity and I’ve started making a FPS.
I made a script attached to my camera with a raycast to detect collision. I made a sphere with an “Enemy” tag. I want my script to check if the object the raycast detects is tagged “Enemy” an do something in consequence. But it just doesn’t work for no reason at all (And I tried many things).

    void Update()
    {
       

        //Raytracing
        Vector3 rayOrigin = this.transform.position;
        Vector3 direction = this.transform.forward;
        RaycastHit hit;
        if (Physics.Raycast(rayOrigin, direction,out hit, Mathf.Infinity)){
            Debug.Log(hit.collider.tag); //Display "Enemy", so the game knows the tag of the object
            if (hit.collider.tag == "Enemy")
            {
                Debug.Log("It works"); //But this doesn't display
            }
        }
        else
        {
            Debug.Log("No collision");
        }
    }

When my raycast touches the sphere it should display “It works” but it doesn’t. What is really strange is that when my Raycast collides with the sphere it displays “Enemy” so it DOES know that its tag is enemy (because of the Debug.Log(hit.collider.tag), but the if statement doesn’t work and Unity never display “It works”. I double checked and it’s not a matter of typo neither. Can somebody has any clue why it’s happening ? I hope I gave enough infos. Thanks in advance !

Ok so… hum. I made a tag with a space after it so that’s why it didn’t work. I hate myself lmao.
Well if you have the same problem as me, make sure to write your tags properly, because unity doesn’t show you whitespace characters. Don’t bother to answer, it is working now. I’m just keeping the topic if people have the same problem.

I recommend just skipping them altogether. Below are three methods. An empty class representing the tag, a basic enum that represents the tag, and a more advanced enum approach that allows for multiple tags at once (this one requires you to number them in the pattern shown).

Best of all the problem you discovered cannot happen with these since none of them are strings.

Empty Class

public class Enemy : MonoBehaviour {}
if (hit.collider.GetComponent<Enemy>())

Class Enum

public enum Tags
{
    None,
    Enemy
}

public class MyTags : MonoBehaviour
{
    public Tags tags;
}
if (hit.collider.GetComponent<MyTags>().tags == Tags.Enemy)

Multiple Tags using Class Enum

[Flags]
public enum Tags
{
    None = 0,
    Enemy = 1,
    Red = 2,
    Blue = 4
}

public class MyTags : MonoBehaviour
{
    public Tags tags;
}
if (hit.collider.GetComponent<MyTags>().tags == Tags.Enemy | Tags.Blue)
3 Likes

I generally like to push as much “business logic” into the class as well. So it seems MyTags could have methods for IsEnemy and whatever the combination of tags shown is defining. Especially helpful if the rules governing things ever changes because one just changes the definition in the class not in each spot the tag is compared.

1 Like