• Unity
  • Returning The Length Of An Animation

Hi all,

I'm trying to return the length of an animation that is playing so that I can gradually increase the speed of an object as the animation plays. I found this thread when searching the forum for previous posts:
Unity: how to get the duration of an animation?

However I get an error that says "The type name 'Duration' does not exist in the type 'Animation' " when I try to use Animation.Duration. What am I missing here?

Related Discussions
...

Spine.Animation definitely has a duration attribute. Changing Animation to Spine.Animation should sort out if you're mixing up Spine's Animation Type with Unity's.

What is the correct syntax? I've been setting the animations using:

skeletonAnimation.AnimationName = "Walk";

In order to use Spine.Animation.Duration I need to set an object reference. I imagine I would need to do this through setting the animation name to a variable, and then calling the variable with Spine.Animation.Duration.

This is the best my monkey brain can come up with:

myAnim = skeletonAnimation.AnimationName = "Walk";
Debug.Log ( Spine.Animation.Duration.myAnim );

You seem to be confused with how C# works.

You need to find the animation object. That can be found through:

var myAnimation = skeletonAnimation.Skeleton.Data.FindAnimation("Walk");

Then you can get the duration.

float duration = myAnimation.Duration;

I highly recommend Unity's official tutorials if you're new to scripting: https://unity3d.com/learn/tutorials/s/scripting

Thanks for the help! Yeah, I'm still learning C#. I've gone through those tutorials in the past but perhaps I should read through them again.