Animation playing while holding button

Hello
I have an Animation and I want it to start when the button is held and stop when i leave the button
its 2 state Animation so its very sample
my codes:

private Animator anim;
private bool isPressed;

void Start()
{
    anim = GetComponent<Animator>();
}

void Update()
{
    if (Input.GetMouseButton(0))
    {
        isPressed = true;
        Anmie();
    }
    else
    {
        isPressed = false;
    }
    anim.SetBool("Idle", !anim.GetBool("Idle"));
}

public void Anmie()
{
    anim.SetBool("Idle", !anim.GetBool("Idle"));
    Debug.Log("Anmie Start");
}

what annoying me is that the Debug show the Animation is playing when I hold the button wich it dosn’t.

if(Input.GetKey(KeyCode.P))
{
animator.SetBool(“Run”,true);
}else
{
animator.SetBool(“Run”,false);
}

Try this:

private Animator anim;
private bool isPressed;

void Start()
{
    anim = GetComponent<Animator>();
}

void Update()
{
    // The purpose of the justPressed, justReleased and justChanged variables is to increase readability
    bool justPressed  = Input.GetMouseButtonDown(0);
    bool justReleased = Input.GetMouseButtonUp(0);
    bool justChanged  = justPressed || justReleased;
    
    if (justPressed)
        isPressed = true;
    
    if (justReleased)
        isPressed = false;
        
    if (justChanged)
    {
        anim.SetBool("Idle", isPressed);
        
        // Logs either "Idle Animation Started" or "Idle Animation Ended"
        Debug.Log("Idle Animation " + (isPressed ? "Started" : "Ended"));
    }
}

You are executing the Anmie() method every frame of the Update, this means that the animation is not visually playing because it is starting over every frame.
You need to add a condition to check if the function is already happening before executing it, so it doesn’t loop over itself.

Remember, everything that is in the Update without a condition preceding it will execute no matter what, avoid this situation since it is a bad practice and will give you problems in the future.

Change:

void Update()
{
    if (Input.GetMouseButton(0) && !isPressed) //We get the input from the player and simultaneously checks if the animation is already playing, avoiding a loop
    {
        isPressed = true;
        Anmie();
    }
}

I would continue the code however you should change it completely, in my opinion you should have a function for your whole dictionary of animations, similar to this:

private void AnimatorWithPriorities(string animationRequested)
{
    if (clipName != animationRequested)
    {
        clipName = animationRequested;
        switch (clipName)
        {
            case "Death":
            animator.Play(clipName);
            break;
            case "Spawn":
            animator.Play(clipName);
            break;
            case "Walk":
            animator.Play(clipName);
            break;            
        }
    }
}

ERORE
this is the entire transaction if you wondered