Play animation a specific number of times
I'm currently trying to have my animation run multiple times after a click on the object. So for example, my animation runs for 1 second and i'd like for it to play 5 times.
public Animator anim;
void Start () {
anim = GetComponent<Animator>();
}
void OnMouseDown() {
for (int i = 0; i < 5; i++)
{
anim.Play("wave", -1, 0f);
}
}
This however does not function as i intended. Hope someone here can help me out!
Sincerely,
Answer by Mikilo · Sep 01, 2015 at 09:08 AM
Hello.
You can use Coroutine.
Like:
void Start ()
{
anim = GetComponent<Animator>();
StartCoroutine(PlayAnimInterval(5, 1F));
}
private IEnumerator PlayAnimInterval(int n, float time)
{
while (n > 0)
{
anim.Play("wave", -1, 0F);
--n;
yield return new WaitForSeconds(time);
}
}
Thanks for the reply. Unfortunately, the animation continues to play despite the changes. It continuously plays.
Are you sure you want to use Animator ins$$anonymous$$d of a Animation? With Animation you can call .Stop() after the loop.
Hmm that actually worked.i used animation ins$$anonymous$$d of animator. thanks for the help!
Your answer
![](https://koobas.hobune.stream/wayback/20220612002231im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Synchronizing multiple sprite animations? 0 Answers
Animating multiple materials? 2 Answers
Unity animation tools 0 Answers
My game Object is bigger when runninng the game than in edit view 1 Answer