Null Reference Exception issue getting the tag of an object hit by Raycast

Basically, I'm trying to detect an object that the player clicked on in a 3d space. Then if the object is tagged as a Target (Tag = "Target"), the script does something. However, I'm running into a Null Reference Exception that says "Object reference not set to and instance of an object"

Here's the code:

function Update () 
{
    var ray: Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    var hit: RaycastHit;

    if (Input.GetMouseButtonDown(0))
    {

        if (hit.collider.gameObject.tag == "Target") 
        {
            print ("Hit!");
        }
        else
        {
            print ("Miss");
        }
    }
}

If I remove the 'If' that checks what the tag of the collided object is, then it seems to work. (And yes, I do have an object tagged as "Target")

I'm still fairly new to scripting, so I'm hoping it's something small that I overlooked.

Thanks

You almost have it right right. You have a ray to cast and something to hold the hit information. But you haven't actually run the ray cast.

See Physics.Raycast for details. I've updated your code with comments.

function Update () 
{
    var ray: Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    var hit: RaycastHit;

    // In the next test, if the mouse button is down and the raycast
    // resulted in detection, you know that the hit variable has a value.
    if (Input.GetMouseButtonDown(0) && Physics.RayCast(ray, hit, 100))
    {
        if (hit.collider.gameObject.tag == "Target") 
        {
            print ("Hit!");
        }
        else
        {
            print ("Miss");
        }
    }
}

You need to do a raycast. Your RaycastHit variable contains nothing, because it hasn't been put into a raycast function.