Locked door and key to open it script

I got a key and a door. The door is locked and should be opened only when you found the key.

this is the code I got for the key and the door. (2 separate codes)

//Door script

private var open : boolean = false;

var openDoorAnimationString : String;
var closeDoorAnimationString : String;

var buttonTransform : Transform;
var distToOpen : float = 6;

private var playerTransform : Transform;
private var cameraTransform : Transform;

var openDoorSound : AudioClip;
var closeDoorSound : AudioClip;

function Awake () {
	playerTransform = GameObject.FindWithTag("Player").transform;
	cameraTransform = GameObject.FindWithTag("MainCamera").transform;
	if(open)
		animation.Play(openDoorAnimationString);
}

function Update () {
	var alreadyChecked : boolean = false;
	if (Vector3.Distance(playerTransform.position, buttonTransform.position)<= distToOpen)
	if (Input.GetKeyDown("e") && !animation.isPlaying)
	{
		if(open)
		{
		animation.Play(closeDoorAnimationString);
		open = false;
		alreadyChecked = true;
		if(closeDoorSound)
			audio.PlayOneShot(closeDoorSound);
		}
		if(!open && !alreadyChecked)
		{
		animation.Play(openDoorAnimationString);
		open = true;
		if(openDoorSound)
			audio.playOneShot(openDoorSound);
		}
	}
}








//Key Script
#pragma strict
private var open : boolean = false;

var pickupSound :AudioClip ; //when picking up the key the sound
var audioPos :Vector3 ;

var liftKeyAnimationString : String;  
var lowerKeyAnimationString : String;

var buttonTranform : Transform;
var distToOpen : float = 6;

private var playerTransform : Transform;
private var cameraTransform : Transform;

function Awake ()
{
	playerTransform = GameObject.FindWithTag("Player").transform;
	cameraTransform = GameObject.FindWithTag("MainCamera").transform;
	if(open)
		animation.Play(liftKeyAnimationString);
	
}

function OnTriggerEnter(Collider)
{
	AudioSource.PlayClipAtPoint(pickupSound, audioPos);
	Destroy(gameObject);
	print("You got the Key");
}

function Update () 
{
	var alreadyChecked : boolean = false;
	if (Vector3.Distance(playerTransform.position, buttonTranform.position)<= distToOpen)
	if (Input.GetKeyDown("e") && !animation.isPlaying)
	{
		if(open)
		{
			animation.Play(lowerKeyAnimationString);
			open = false;
			alreadyChecked = true;
		}
		if(!open && !alreadyChecked)
		{
			animation.Play(liftKeyAnimationString);
			open = true;
		}
	}
	transform.Rotate(0, 50 * Time.deltaTime, 0);
}

If you want the game to know whether you’ve gotten a key, make a variable like

var HaveKey: boolean

and when you ‘pick up’ the key, set HaveKey to true. Then, upon collision with the door, if you press the open door button and HaveKey is true, you can open it.