- Home /
Making an animated door, that will be reused over a level, need to reference its parent
Hey Guys, I am making an animated door which I am going to scatter throughout a level I am working on.
What I currently have is a prefab structured as follows:
door=empty game object
--doorMesh=model & anim
----trigger
And the code attached to my trigger this:
void OnTriggerStay (Collider info)
{
if(info.tag == "Player") {
if (Input.GetButtonDown("Fire2"))
{
//animation door open
GameObject.Find("cellDoor").animation.Play("Take 001");
}
}
}
As you can see, it only works if you have just one door. What is needed is a way to reference the doorMesh a step above in the hierarchy.
Any help would be greatly appreciated :D
Answer by Waz · Jun 24, 2011 at 12:14 AM
transform.parent
is the component's parent in the object hierarchy. Personally, I prefer ownership to pass down the object tree, so I'd put the collider on the object you marked empty and do:
void OnTriggerStay (Collider info)
{
if(info.tag == "Player") {
if (Input.GetButtonDown("Interact"))
{
transform.Find("doorMesh").animation.Play("Open");
}
}
}
I'd name your key inputs and animations meaningfully too. Then you don't need the comment. Comments often (but not always) indicate poorly named/structured code.