- Home /
Collect Key and open door
Bonjour people :D okay, i have a little piece of code that i need fixing :P 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
Answer by farooqaaa · Mar 25, 2012 at 03:49 PM
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");
}
}
Answer by leonalchemist · Mar 25, 2012 at 03:39 PM
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
Your answer
![](https://koobas.hobune.stream/wayback/20220613065523im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Delaying GUITexture!!! 3 Answers
creating a GameObject Instead of a Cube 1 Answer
C# Capture Keypress 3 Answers
Exiting witha GUI Button 1 Answer