How can my Raycast collide with a collider?

My Code:
using UnityEngine;
using System.Collections;

public class Door_MAnager : MonoBehaviour
{

private bool open = false;
private bool GUIshow = false;

private Vector3 direction;
public float lenght;
private Vector3 spawn;
public GameObject door;
private RaycastHit hit;

void Start()
{
	spawn = GameObject.Find("FPV_Final").GetComponent<Living_conditions>().spawn;
}

void Update () 
{
	direction = transform.TransformDirection(Vector3.forward);

	if (Input.GetKeyDown ("escape")) 
	{
		Application.Quit ();
	}
	else if (Physics.Raycast (spawn, direction, lenght)) 
	{
		if (hit.collider.gameObject.tag == "Door") 
		{
			GUIshow = true;
			if (Input.GetKeyDown ("e") && open == false) 
			{
				door.GetComponent<Animation> ().Play ("OpenDoor");
				open = true;
				GUIshow = false;
			} 
			else if (Input.GetKeyDown ("e") && open == true) 
			{
				door.GetComponent<Animation> ().Play ("CloseDoor");
				open = false;
				GUIshow = false;
			}
		}
	} 
	else 
	{
		GUIshow = false;
	}
}

void OnGUI()
{
	if(GUIshow == true && open == false)
	{
		GUI.Box(new Rect(100, 25, Screen.width / 2, Screen.height / 2), "Press E to open door");
	}
}

}

my Problem:
In the Game NOTHING happens. no errors, no opening or closing doors, no animation being played

2 things I noticed;

  1. Is there a particular reason you are doing everything in an ‘else if’ statement rather than just an ‘if’ statement?
  2. You are casting the ray from a point at the beginning of the scene, so if your character is moving it’s not going to be casting a ray from the character position, but rather from where the character started.

However, the reason nothing is happening is because you haven’t actually assigned the ‘hit’ variable to anything. All you have done is declare it.

if (Physics.Raycast (spawn, direction, lenght))

//should be

if (Physics.Raycast (spawn, direction, out hit, lenght))