Troubles with raycasting to find tags

Hi all,
I’ve been trying to make a crosshair that changes color when it hovers over objects with particular tags (i.e, “Friend” and “Enemy” tags). I had a look around, and saw you needed to raycast to find such things. Raycasting’s unfamiliar territory for me, so I’m kinda flying blind here. This is the script I have:

var crosshairTexture : Texture2D;
var crosshairRange = 200;

private var hit : RaycastHit;
private var facingDirection = transform.TransformDirection(Vector3(0,0,1));

function Update () 
{
    if (Physics.Raycast(transform.position, facingDirection, hit, crosshairRange))
    {
        if(hit.collider.gameObject.tag != "Untagged")
        {
            if (hit.collider.gameObject.tag = "Enemy")
            {
                crosshairTexture.color = Color.(1, 0, 0, 0.5);
            }

            else if (hit.collider.gameObject.tag = "Friend")
            {
                crosshairTexture.color = Color.(0, 1, 0, 0.5);
            }

            else
            {
                crosshairTexture.color = Color.(1, 1, 1, 0.5);
            }
        }
    }
}

In line 13 - if (hit.collider.gameObject.tag = “Enemy”) it comes up with the error “expecting ), found =.” and also “Unexpected token Enemy.” I’ve tried a few different versions of the hit.collider.gameObject.tag thing, none of which have worked. I know this is probably just a simple mistake (last time I forgot to capitalize a ‘u’) but I can’t for the life of me figure it out.

PS. Somebody told me that in the ‘if’ statements there needed to be == instead of just =, however when I tried that it came up with 27 errors…

Any help would be appreciated!

Try this:

var crosshairTexture : Texture2D;
var crosshairRange = 200;

private var hit : RaycastHit;
private var facingDirection = transform.TransformDirection(Vector3(0,0,1));

function Update () 
{
    if (Physics.Raycast(transform.position, facingDirection, hit, crosshairRange))
    {
        if(hit.collider.gameObject.CompareTag("Untagged"))
        {
            if (hit.collider.gameObject.CompareTag("Enemy"))
            {
                crosshairTexture.color = Color(1, 0, 0, 0.5);
            }

            else if (hit.collider.gameObject.CompareTag("Friend"))
            {
                crosshairTexture.color = Color(0, 1, 0, 0.5);
            }

            else
            {
                crosshairTexture.color = Color(1, 1, 1, 0.5);
            }
        }
    }
}

You do need to use ==, and not =. It’s not like this is optional or something, they’re entirely different.

= is used to assign something to a variable. == is used to compare something.

You also want to use CompareTag, I think.

Excellent! Thanks sangi, that worked like a charm! Also, thanks for the clarification of the = symbols xomg, I couldn’t tell the difference between the two before.

Right… now on to the next problem :stuck_out_tongue: