Animation with string and attack animation problem

Hi,
I’m having some problems with the transition of my Idle Animation and Attack Animation, when I press the attack button the animation starts and in milliseconds it changes to idle animation, and keeps changing from idle to attack infinitely… how i can stop this string to play my attack animation?

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

public class PlayerAnimation : MonoBehaviour{
   
    private Animator anim;

    public string[] staticDirections = {"IdleR", "IdleL", "IdleL", "IdleL", "IdleL", "IdleR", "IdleR", "IdleR"};
    public string[] runDirections = {"RunR", "RunL", "RunL", "RunL", "RunL", "RunR", "RunR", "RunR"};

    int lastDirection;

    private void Awake(){
        anim = GetComponent<Animator>();

        float result1 = Vector2.SignedAngle(Vector2.up, Vector2.right);
        float result2 = Vector2.SignedAngle(Vector2.up, Vector2.left);
        float result3 = Vector2.SignedAngle(Vector2.up, Vector2.down);
        }

    public void SetDirection(Vector2 _direction){
        string[] directionArray = null;

        if(_direction.magnitude < 0.01){
            directionArray = staticDirections;
        }
        else{
            directionArray = runDirections;
            lastDirection = DirectionToIndex(_direction);
        }
        anim.Play(directionArray[lastDirection]);
    }

    private int DirectionToIndex(Vector2 _diretion){
        Vector2 norDir = _diretion.normalized;
        float step = 360 / 8;
        float offset = step / 2;
        float angle = Vector2.SignedAngle(Vector2.up, norDir);
        angle += offset;
        if(angle < 0){
            angle += 360;
        }

        float stepCount = angle / step;
        return Mathf.FloorToInt(stepCount);
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerCombat : MonoBehaviour{

        Animator animator;

        int clicks;
        bool canClicks;

        void Start(){
                animator = GetComponent<Animator>();
                clicks = 0;
                canClicks = true;
        }

        void Update(){
                //if(Input.GetKeyDown(KeyCode.Space)){
                if(Input.GetMouseButtonDown(0)){
                        Attack();
                }
        }

        void Attack(){
                if(canClicks){
                        clicks++;
                }
                if(clicks == 1){
                        animator.SetInteger("Attack", 1);
                }
               
        }

        public void checkAttack(){
                canClicks = false;

                if(animator.GetCurrentAnimatorStateInfo(0).IsName("AttackA") && clicks == 1){
                        animator.SetInteger("Attack", 0);
                        canClicks = true;
                        clicks = 0;
                }
                else if(animator.GetCurrentAnimatorStateInfo(0).IsName("AttackA") && clicks >= 2){
                        animator.SetInteger("Attack", 2);
                        canClicks = true;   
                }
                else if(animator.GetCurrentAnimatorStateInfo(0).IsName("AttackB") && clicks == 2){
                        animator.SetInteger("Attack", 0);
                        canClicks = true;
                }
                else if(animator.GetCurrentAnimatorStateInfo(0).IsName("AttackB") && clicks >= 3){
                        animator.SetInteger("Attack", 3);
                        canClicks = true;
                }
                else if(animator.GetCurrentAnimatorStateInfo(0).IsName("AttackC")){
                        animator.SetInteger("Attack", 0);
                        canClicks = true;
                        clicks = 0;
                }
        }
}

Try opening the Animator window so you can see what states it is transitioning in real time.

Then trigger the attack and see where it goes. Make sure you have the attack transition checked to have “Has Exit Time” (i.e., select the transition out to idle, see what the properties on it are).