Opening a door with a key

I have got a script that works to unlock and open a door, but I was wondering if there is a way to pick up a key first to open the door and it won’t work without that key. I can show you the code I have already, please let me know how I can do this.

//Player:
var doorScript : Door;

function Update () {
	var hit : RaycastHit;
	Debug.DrawRay(transform.position, transform.forward * 2, Color.red);
	if (Physics.Raycast(transform.position, transform.forward, hit, 2)) {
		if ((hit.collider.gameObject.tag == "Door") && (Input.GetButtonUp("Action") ) ) {
			if (doorScript.doorState == DoorStates.locked) {
				doorScript.doorState = DoorStates.unlocked;	
			} else if (doorScript.doorState == DoorStates.unlocked) {
				doorScript.Open();
			}
		}
	}
}

//Door Trigger:
var doorScript : Door;

function OnTriggerEnter (col : Collider) {
	if ((col.gameObject.tag == "MainCamera") && (doorScript.doorState == DoorStates.unlocked) ) {
		doorScript.Open();
	}
}

//Door:
enum DoorStates {open, unlocked, locked};
var doorState : DoorStates;

function Awake() {
	doorState = DoorStates.locked;
}

function Update () {
	Debug.Log(doorState);
}

function Open() {
	animation.Play("Open");
	doorState = DoorStates.open;
}

Well all you need is another variable:

	Player: var doorScript : Door;

	var hasKey: boolean=false;

	function foundAKey()
	{
	hasKey=true;
	}

	function Update () 
	{ 
	var hit : RaycastHit; 
	Debug.DrawRay(transform.position, transform.forward * 2, Color.red); 

	if (Physics.Raycast(transform.position, transform.forward, hit, 2)) 
		{ 
			if ((hit.collider.gameObject.tag == "Door") && (Input.GetButtonUp("Action") ) ) 
			{ 
				if (doorScript.doorState == DoorStates.locked && hasKey )  <-----
				{ 
					doorScript.doorState = DoorStates.unlocked; 
				} else if (doorScript.doorState == DoorStates.unlocked) 
				{ 
					doorScript.Open(); 
				} 
			} 
		} 
	}

	Door Trigger: var doorScript : Door;

	function OnTriggerEnter (col : Collider) 
	{ 
		if ((col.gameObject.tag == "MainCamera") && (doorScript.doorState == DoorStates.unlocked) ) 
		{ 
			doorScript.Open(); 
		} 
	}

	Door: enum DoorStates {open, unlocked, locked}; var doorState : DoorStates;

	function Awake() { doorState = DoorStates.locked; }

	function Update () { Debug.Log(doorState); }

	function Open() { animation.Play("Open"); doorState = DoorStates.open; }

Then have another object that is a key that when is picked up calls “foundAKey()”.

You have to assign a boolean variable to your Player, that says if the key has been already picked up or not. At the beginning this variable will be equal to false; When the Player enters in a collision with the key, it will turns into true. Then, you add this variable’s truth as a mandatory event to open the door. Something like this:

//Player:
var doorScript : Door;
var have_key = false;

function Update () {
    var hit : RaycastHit;
    Debug.DrawRay(transform.position, transform.forward * 2, Color.red);
    if (Physics.Raycast(transform.position, transform.forward, hit, 2)) {
       if ((hit.collider.gameObject.tag == "Door") && (Input.GetButtonUp("Action") ) ) {
         if (doorScript.doorState == DoorStates.locked) {
          doorScript.doorState = DoorStates.unlocked; 
         } else if (doorScript.doorState == DoorStates.unlocked && have_key) {
          doorScript.Open();
         }
       }
    }
}

function OnCollisionEnter (theCollision : Collision){
    if (theCollision.gameObject.tag == "Key")
       have_key=true;

}