how to animate a child

Hello.
I must animate my player. My ‘player’ has a ‘child’ that is the model imported(with the animation). My ‘player’ has all the scripts, and the ‘child’ comes from a prefab.

How do i animate this ‘child’ with a script in the ‘player’ ???

THanks! =D

Tons of ways of doing this, but without knowing exactly how your stuff is constructed here are some options.

If the parent is doing the instantiation of the prefab, then call this when the prefab is made and linked:

animation = GetComponentInChildren(typeof(Animation)) as Animation;

Or

GameObject newGO = Instantiate(thePrefab, ....) as GameObject;
animation = newGO.GetComponent(typeof(Animation)) as Animation;
...

My above examples are borrowing the already existent “animation” member variable in all MonoBehaviours, by the way. Unless the parent GameObject already has an Animation component, which in your case probably does not, then you can just borrow the existing variable.

i have the Player and inside him in the hierarchy i have the 3d model to animate. i want to use the script which is in the Player to animate the 3D model…

something like
model.animation.CrossFade(“anim1”);

how??
didn’t work as u said…

Thank yoU!!!
:smile:

EDIT:
YES, the parent and the child(model) has animation component…

Did you try it as

animation.CrossFade("anim1");

??

Can you post a screenshot showing your object hierarchy?

The Aviao is where i have my scripts. In its child is where i found an animation. but both have animation component.

THx!

313270--11061--$myhierarchy_754.jpg

You can get a child object’s transform using transform.Find. The transform has an animation property that you can use to start playback:-

var child = transform.Find("aviaoPrefab/aviao");
child.animation.Play();