Need some help with JS raycasting hitting a certain tagged object

I am having problems with my raycast code in JS sending back a null ref. error every time I check to see if it hit the door object tagged “door”. The raycaster is in front of my FPController, and I’m needing to check if I’m in front of the door when I press the c key. If I am in front of door within specified range, allow me to open door with another key. If not, I can’t open door. Hope this makes sense and I can get some help here. Thanks in advance.

#pragma strict
var raycastDist: float = 2.0;


function Update () 
{
	var hit: RaycastHit;
	Debug.DrawRay (transform.position, transform.forward, Color.green);
	
	if(Input.GetKeyDown("c"))
	{
		if(Physics.Raycast(transform.position, transform.forward, raycastDist))
		{
			if(hit.collider.gameObject.tag == "door")
			{
				print("hit door detected");
			}
			
		}
		else
		{
			print("no hit door detected");
		}

	
	
	}

	
}

You are not passing your RaycastHit structure into your Raycast() function, so even when the Raycast succeeds, the ‘hit’ variable is not initialized. You need to call it this way:

   if (Physics.Raycast(transform.position, transform.forward, hit, raycastDist))

The signature of the call you want to make is:

   static function Raycast(origin: Vector3, direction: Vector3, hitInfo: RaycastHit, distance: float = Mathf.Infinity, layerMask: int = DefaultRaycastLayers): bool;