Search Unity

Calling an Animation in Child Object

Discussion in 'Animation' started by the_terrible, Jan 9, 2019.

  1. the_terrible

    the_terrible

    Joined:
    Nov 4, 2015
    Posts:
    19
    Hi all,

    I've been calling different Spine animations I've created using the following code:
    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using Spine.Unity;
    4. using Spine;
    5. public class Movement : MonoBehaviour
    6. {
    7.      TrackEntry activeTrackEntry;
    8.      SkeletonAnimation skeletonAnimation;
    9.  
    10.      public string WalkToChaseEndEventName = "WalkToChaseEnd";
    11.  
    12.      void Start()
    13.      {
    14.           skeletonAnimation = GetComponentInChildren<SkeletonAnimation>();
    15.           skeletonAnimation.state.Event += HandleEvent;
    16.      }
    17.  
    18.      void HandleEvent(Spine.TrackEntry entry, Spine.Event e)
    19.      {
    20.           if (e.Data.Name == WalkToChaseEndEventName)
    21.           {
    22.                activeTrackEntry = skeletonAnimation.AnimationState.SetAnimation(0, "Chase", true);
    23.           }
    24.      }
    25. }
    The animator script imported from Spine is set to a child of the object this code is attached to which is why I'm using GetComponentInChildren. The problem I'm having is that I also have another object, ContextObject, set as a child of this object. ContextObject contains its own imported animations from Spine that I want to call as well. How do I call an animation to be played for the child ContextObject, not its parent?