Collect Key and open door

Bonjour people :smiley: okay, i have a little piece of code that i need fixing :stuck_out_tongue: all i want is that when i pick up my key, the door animation plays could anyone please help

var gate = false;

function OnControllerColliderHit( col: ControllerColliderHit)

{

	//gate code here

	if(col.gameObject.tag == "key")

{

	gate = true;

	Destroy(col.gameObject);

	gameObject.Find("Gate",animation.Play("GateOpen"));
	
}

}

Can anyone help me >>

Thanks

Its GameObject.Find() with a capital β€œG” and it only has one parameter. You can’t call the animation script from the second parameter. Your code should look like this:

var gate = false;
function OnControllerColliderHit( col: ControllerColliderHit)
{
    //gate code here
    if(col.gameObject.tag == "key")
    {
        gate = true;

        Destroy(col.gameObject);
        GameObject gateObj = GameObject.Find("Gate");

        gateObj.animation.Play("GateOpen");
    }
}

Also, a better practice is to save the gate GameObject in a public variable and assign its reference in the editor.

// Assign the Gate gameobject to this in the Editor
public var gateObj : GameObject;
var gate = false;

function OnControllerColliderHit( col: ControllerColliderHit)
{
    //gate code here
    if(col.gameObject.tag == "key")
    {
        gate = true;

        Destroy(col.gameObject);

        gateObj.animation.Play("GateOpen");
    }
}

Im not 100% sure im kind of a rookie, but if that code is linked to the key then anything after the line where u destroy the gameObject isnt going to happen since its gone from the scene.
so possibly try moving the animation code b4 the destroy code or try moving the animation code to another object on the door itself possibly unless this script is on the main player

Otherwise if it still doesnt work maybe try changing

function OnControllerColliderHit( col: ControllerColliderHit)

to

function OnTriggerEnter( col: Collider)

then of course check the onTrigger ON