How to change the animation when the timer comes to zero in Unity

When I click on the player1,the timer should start and play the animation(its working fine).When the timer comes to zero the animation changes and play “DropBothFork01” animation.I have written the timer in update().But the problem is that when the timer stops the “DropBothFork01” animation is playing continuosly.(i know update()that calls continuosly).What is the solution for this? is it possible to do this without update?

 public void Update(){

 if(OnMouseDownnew)
    {
        timeLeft -= Time.deltaTime;

        if (timeLeft <= 0.0f)

        {
            gamobjtxt3.text = "Time is over";
           
            animation.Play("DropBothFork01"); 

           
        }  
        else{
            gamobjtxt3.text = "Time left = " + (int)timeLeft + " seconds";
            }
        }

}

    void OnMouseDown () {
    if(gameObject.name=="player1")
    {
    Camera.main.GetComponent<testscript>().animations();
    OnMouseDownnew=true;
    }}

I think that if you were to change line #12 to This:

animation.PlayOneShot("DropBothFork01", PlayMode.StopAll);

it might work.

also you might want to check if the animation is looping if it is make it not loop then try again.

Hope this helps -T.D.M.3

You can stop the animation once the timer is completed. The code in line 17 should execute at that particular moment. Try the following statement there:

animation.Stop();

Generally speaking, you can stop animations using the Stop method. Check Unity - Scripting API: Animation.Stop for additional details.