Unlocking Gate with Key

OK Got something solid to work with but not sure about getting the functionality of it working. I have a key hanging on a wall with a box collider and this script attached:

EDIT I updated my Key pickup script so now it works when you press "e".

    private var gotKey : boolean = false;

var keyPickupSound : AudioClip;

function OnTriggerStay (){

    if (Input.GetButtonDown("Pickup")){
        gotKey = true;
        Destroy(gameObject);

    //Play Key Pickup Sound
    if (keyPickupSound)
    AudioSource.PlayClipAtPoint(keyPickupSound, transform.position);

    print("You Picked Up The Gate Key");
    }
}

I THINK and underline that think it's picking it up although I'm very fuzzy on how the whole pickup stuff works. I'm pretty sure I'm doing something wrong as it's supposed to pick up the key when you press "e" but instead it seems to do it when you get very close to it... so I'm not sure if it's actually working right or not.

OK so that's problem 1.

Problem 2

Here is the script attached to my Gate. I'm not sure how to get it so it recognizes that the FPS player has picked up the key and then how to get that to play the "Gate Unlock" sequence.?

enum oldGateStates {open, locked, unlocked};
var oldGateState : oldGateStates;
var oldGateUnlockSound : AudioClip;
var oldGateSoundOpen : AudioClip;

function Awake() {
    oldGateState = oldGateStates.locked;
}

function Update () {
}

function Unlock() {
    animation.Play("GateUnlock");
    oldGateState = oldGateStates.unlocked;
    audio.PlayOneShot (oldGateUnlockSound);

}

function Open() {
    animation.Play("GateOpen");
    oldGateState = oldGateStates.open;
    audio.PlayOneShot (oldGateSoundOpen);

}

Edit I forgot to add the part in my Player script that should open the gate only if my player has the key:

var hit6 : RaycastHit;
    if (Physics.Raycast(transform.position, transform.forward, hit6, 3)) {
        if ((hit6.collider.gameObject.tag == "OldFarmGate") && (oldFarmGateScript.oldGateState == oldGateStates.locked) ) {
            oldFarmGateScript.Open();
        }
    }

Problem 1) In this line of code...

if (Input.GetButtonDown("Pickup"))
    gotKey = true;
    Destroy(gameObject);

You are missing the brackets on your if check. As such, the input is still checking for a button pressed, but the Destroy(gameObject) is getting called regardless of the checks because it is outside the default brackets. Thats why the key is disappearing when you run into the trigger. You actually haven't picked it up until you press 'E'.

    if (Input.GetButtonDown("Pickup")) {
         gotKey = true;
         Destroy(gameObject);
    }

That should fix that...

Problem 2)

var hit6 : RaycastHit;
if (Physics.Raycast(transform.position, transform.forward, hit6, 3)) {
    if ((hit6.collider.gameObject.tag == "OldFarmGate") && (oldFarmGateScript.oldGateState == oldGateStates.locked) ) {
        oldFarmGateScript.Open();
    }
}

If this code is in the same script and "gotKey" is a global variable, just add another check.

var hit6 : RaycastHit;
if (Physics.Raycast(transform.position, transform.forward, hit6, 3)) {
    if ((hit6.collider.gameObject.tag == "OldFarmGate") && (oldFarmGateScript.oldGateState == oldGateStates.locked) )
        if(gotKey){
        oldFarmGateScript.Open();
    }
}

If "gotKey" is in another script you are going to have to send or receive a message from that script in order to get the variable. There are lots of different methods for handling this, so look into it a little.