Prevent script from taking input until animation has finished?

Sorry for my badly worded title, cannot really think of good, quick wording for my situation. Anyway, I am pretty new with programming in Unity (and in general) and I ran into an issue. I created a script that checks if the player has the mouse hovering over an object. If the mouse is hovering, it checks for the player’s distance from the object and for input from the mouse (left click). If the player is close enough to the object that he script is attached to and left clicks, an open animation plays and a bool changes to the opposite. If the object is clicked again, the script runs the if statement with the corresponding bool and runs the corresponding animation, which is a close animation the second time.

My issue is, however, is that if the player spam clicks, the animations are interrupted by the next animation. All my animations are 0:10 long at 30 frames, just in case this is useful. I am wondering if there is a way that I can not check for input until an animation finishes. Thanks in advanced.

Script:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;


public class ContainerOpen : MonoBehaviour
{
    private bool open;
    private float distance;

    void Awake()
    {
        distance = 3;
        open = false;
    }

    void OnMouseOver()
    {
        if (Input.GetMouseButtonDown(0))
        {
            Vector3 playerPosition = GameObject.Find("FPSController").transform.position;
            if (Vector3.Distance(playerPosition, transform.position) <= distance)
            {
                if (open == true)
                {
                    GetComponent<Animation>().Play("SlideIn");
                    open = !open;
                }
                else if (open == false)
                {
                    GetComponent<Animation>().Play("SlideOut");
                    open = !open;
                }

            }

        }

    }

}

There’s probably a better way to sample the current animation, but I’m not very familiar with the animation system in general so I’ll offer a different solution: Create a coroutine that prevents interaction while the animation is playing.

private bool interactable = true;

void OnMouseOver()
{
   if(interactable == false)
     return;

  if (Input.GetMouseButtonDown(0))
  {
  Vector3 playerPosition = GameObject.Find("FPSController").transform.position;
  if (Vector3.Distance(playerPosition, transform.position) <= distance)
  {
   GetComponent<Animation>().Play(open == true ? "SlideIn" : "SlideOut");
   open = !open;

   StartCoroutine(StopInteraction);
  }
}
}

IEnumerator StopInteraction()
{
   interactable = false;

   yield return new WaitForSeconds(10);

  interactable = true;
}

You could use Animation Events for this.

Disable input when the animation starts and re-enable it when it finishes.