Bonjour people
okay, i have a little piece of code that i need fixing
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
2 Answers
2
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