Audio In Key Script Not Working

I have created a key and door. The door only opens when the key is obtained by the player. The player has to press “E” to obtain the card. When the player presses “E”, I want a sound played. My script isn’t working so I was hoping someone could be able to help.

#pragma strict

var TheKey : GameObject;
private var playerNextToKey = false;
var KeySound : GameObject;

function Update () 
{
 	if (Input.GetKeyDown(KeyCode.E) && playerNextToKey == true)
	{
	TheKey.active = false;
	}
}

function OnTriggerEnter (theCollider : Collider)
{
	if (theCollider.tag == "Player")
	{
		playerNextToKey = true;
		KeySound.audio.Play();
	}
}

function OnTriggerExit (theCollider : Collider)
{
	if (theCollider.tag == "Player")
	{
		playerNextToKey = false;
		KeySound.audio.Play();
	}
}

Couple of things. The sound will activate when the player is in the trigger box and not when it presses “E”. That’s where you have the audio.Play.

The key statement won’t work because you are using .active.
To disable a whole gameObject use TheKey.SetActive(false); instead. (And true to enable it back).