Why wont my raycast compareTag properly?

I have a ray that shoots out, if it hits anything it spits out a public string of the gameobject it hits and the distance to it.

But to refine it, I created a Tag named “Target”, tag my game object I want the ray to notice, then I made an if statement to compareTag so it will only notice if it hits a game object tagged as a “Target” (Down the road I want this ray ignored completely UNLESS it hits a target, then I want a special bullet to come out, or special effect to play.)

I think my code looks right, but it still spits out any game object to the string, regardless if the gameobject is tagged as a Target or not.

I also notice that it only spits out the else string, “null” when the ray hits NO game object at all, but only when that happens, if it hits an untagged(as “Target”) game object it still prints the name. As if the tag is not a concern at all for it, only whether its hitting a game object or not.

Maybe I am doing this all wrong, but here is my if/else’s for if the ray hits something and if it matches the tag, spit out the gameobject string into the public var and the distance to said gameobject (C#):

void Update()
{
            if (Physics.Raycast(transform.position, (forward), out rayHit))
            {
                if (rayHit.collider.CompareTag("Target"))
                theDistance = rayHit.distance;
                targetObject = (" " + rayHit.collider.gameObject.name);
            }
            else
                targetObject = ("null");
            //hmm why is this only spitting out null if its not aiming at anything?          
}

Physics.Raycast returns bool True if the ray intersects with a Collider, otherwise false. When hitting no object it would go to the else then and print out your null. In your condition comparing target, the if only braces your distance assignment, so object target or not is running that name print out line if it passes the Physics.Raycast condition. Brace both lines. Additionally I would use a layermask, if that makes sense for your project. For example if you have a target type you can assign its layer to target, that way you can use that layermask in the Physics.Raycast, and your Raycast will only consider targets.
Upon further looking I think you want this as well.

if (rayHit.collider.gameObject.CompareTag("Target"))
{
// Whatever
}

Hi mate,
I see 2 things with your code;

  1. The question in your comment is basically asking why you only get a null when the ray cast hits nothing which is because the ray cast is either hitting something or not. Your else may be in the wrong place.

  2. Also the line where your If is located to Check the tag of the object collided by the ray has no brackets to support only showing when the tag Target is hit. Put some brackets around it like so.

              if (rayHit.collider.CompareTag("Target"))
              {
                  theDistance = rayHit.distance;
                  targetObject = (" " + rayHit.collider.gameObject.name);
              }
    

That way you enclose the distance and object name to only occur when the tag is target types.

Based on my understanding of your question.