Unexpected NullReferenceException

For some reason I can’t get my head around this NullReferenceException. I keep getting the same annoying error no matter what I try. The error lies on line 44 (if (hit.collider.tag == “Brick”))) of my code, but I reference the same variable a few lines ahead of the code, the variable is declared in the class scope, so for whatever reason this is happening its unexpected. The way I want this script to work is to instantiate a prefab where the cursor is, when I click. This is not so, because of the Null Ref’s. This is my code:
using UnityEngine;
using System.Collections;

public class BBCursor : MonoBehaviour {
	public Transform Cursor;
	public float yDisp = .35f;
	public float yRotate = 90f;
	public bool rotated = false;
	public AudioSource audioSound;
	public AudioClip placeSound;
	public AudioClip deleteSound;
	RaycastHit hit;
	Ray ray;
	int rotations = 0;
	void Update () {
			rotations += 1;
			rotations -= 1;
		ray = Camera.main.ScreenPointToRay(Input.mousePosition);
		if (Physics.Raycast (ray, out hit) && hit.collider.gameObject.tag != "MainCamera") 
			Cursor.rotation = FindClosestStud(hit.point).transform.rotation;
			Cursor.Rotate(0, yRotate*rotations, 0);
			Cursor.position = FindClosestStud(hit.point).transform.position;
			Cursor.position += new Vector3(0, yDisp, 0);
			audioSound.clip = placeSound;
			audioSound.loop = false;
			Cursor.BroadcastMessage("Placed", SendMessageOptions.DontRequireReceiver);
			if(hit.collider.tag == "Brick")
				audioSound.clip = deleteSound;
				audioSound.loop = false;
				hit.transform.position = Cursor.position;
				hit.transform.rotation = Cursor.rotation;
				hit.transform.parent = Cursor;
				Cursor.BroadcastMessage("Picked", SendMessageOptions.DontRequireReceiver);

	GameObject FindClosestStud (Vector3 input)  
		// Find all game objects with tag Enemy
		GameObject[] gos;
		gos = GameObject.FindGameObjectsWithTag("Stud"); 
		GameObject closest = gameObject; 
		float distance = Mathf.Infinity; 
		Vector3 position = input; 
		// Iterate through them and find the closest one
    	foreach (GameObject go in gos)
			Vector3 diff = go.transform.position - position;
			float curDistance = diff.sqrMagnitude;
			if (curDistance < distance) {
				closest = go;
				distance = curDistance;
		return closest;

You’re checking the results of your raycast hit outside of the if statement regarding it, which means you are trying to get what you hit when you hit nothing. When you don’t hit something, then your hit.collider is null (and possibly hit as well, I can’t remember at the moment). Either check to make sure hit and hit.collider aren’t null, or more likely, move the statement inside your if (raycast) brackets.

how about if Hit.collider.gameobject.tag
or just hit.gameobject.tag

im doubting you tagged the collider you probably tagged the game object. since there is no tag its null.

mark as answered if so.

Perhaps you could try to add some additional validation, such as;

if(hit.collider.tag != null AND hit.collider.tag = "Brick")

Or wrap it in an If statement stating that it should only be called IF hit.collider.tag != null.

Can you tell me where(i.e line) null exception generated in above line of code.

I think it will be generated after “Fire1” key pressed.