RaycastHit only applying for objects with a specific tag.

I’ve been trying to find help for this for three days and still haven’t found any either it’s either to old or doesn’t work on my code, so here’s my code:

var TargetDistance : float;
var AllowedRange : float = 15;

function Update () {
    if(Input.GetButtonDown("Fire1")) {

        var hit : RaycastHit;
            if (Physics.Raycast (transform.position, transform.TransformDirection(Vector3.forward), hit)) {
                TargetDistance = hit.distance;
                    if (TargetDistance < AllowedRange) {
                        hit.transform.gameObject.GetComponent.<Renderer>().material.color = Color.green;
                    }

            }
     }
}

I want to make it so that the RaycastHit only applies on a certain tag.
Unity some words we use for our code such as RaycastHit, it says incorrect spelling on your spelling.

if (Physics.Raycast (transform.position, transform.TransformDirection(Vector3.forward), hit,AllowedRange))
{
if(hit.collider.CompareTag(“Some Tag”))
hit.transform.gameObject.GetComponent.().material.color = Color.green;
}

Something like tha should work, also in the Raycast you can define the range so you don’t have to check later. On the Tags make sure you write them exactly as you have them in editor. Hope that helps. (there are several tutorials about Raycast look around on Learn section)

As far as i know, there is only a filter : LayerMask.

There is a workaround : use RaycastAll.

        //distance is the length of ray
        RaycastHit[] hits = Physics.RaycastAll(transform.position, transform.TransformDirection(Vector3.forward) , distance);

        foreach(RaycastHit hit in hits)
        {
            if(hit.transform.tag.Equals("YourTag"))
            {
                //Do somethings.
            }
        }

Written in C# (i’m not sure about js)