Loading a animation variable

Hi

I am having a problem with getting an animation to play and I am not sure what I am doing wrong.

I am using C Sharp

I have placed the code;

public AnimationClip AttackAnimation;

With my other variables, this is so I can add the animation I want to run in the inspector.

I am trying to do it so when one of my other functions loads, it plays that animation but I cannot seem to get it right.

I have tried things such as;

animation.play(AttackAnimation);

But that doesnt seem to work :slight_smile:

Any advise would be great

Thanks!

At first you need to apply a “Animation” component to your gameobject.and add your attack animation to the animations-array on that component.
In your script, do animation.play(AttackAnimation.name)

Thanks :slight_smile:

So if I add the animation in the inspector, do I still need to declare it in the code?

Or can I just play the animation with the code you provided?

Thanks

I have placed the animation I want to use in the inspector and the object also has a animation component attached to it :slight_smile:

  1. Add animation component to your GO.
  2. Insert the animations in the array of the inspector of the Animation component.
  3. Go to code and write:
Animation anim = GetComponent<Animation>(); 
anim.Play("animationNameHere");

Thank you both very much!!

It was continuing to play the animation but worked out how to do it to just play the once.

Pretty new to any kind of scripting/coding but slowly learning :slight_smile:

Thanks again!