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,

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);
         }
     }