Issue with mecanim playing an animation using setbool

I’m having issues telling the model to use the attack animation. The animation plays, but I am getting a problem where it just plays the animation regardless of the button press and then just stays there. I’ve tried a few different variations to try to get it to work, but I’m new to animations and I’m lost right now. I’ve looked at the api for the setbool and I believe I am using it correctly. Any help would be appreciated. I can include a picture of my mecanim animator if need be.

  //Handles attacking
        if (Input.GetKeyDown(KeyCode.Space))
        {

                //Start animation
                attack = true;
                anim.SetBool("attack", attack);
            

            // ADD IF STATEMENT HERE TO CHECK MAGIC USES....ENCAPSULATE THE SWITCH WITH 
            // THIS STATEMENT THAT WAY IT IS ONLY CHECKING ONCE THE ACTION IS BEING DONE
            // AKA IF SPACE IS PRESSED -> IF MAGIC USES != 0 -> DO ACTION
            if (MagicUses > 0)
            {
                

                //Handle Switching Projectiles/Attacks
                switch (attackNumber)
                {
                    case 1:
                        Rigidbody shot = Instantiate(fireProjectile, transform.position + (transform.forward * 0.5f), transform.rotation) as Rigidbody;
                        shot.velocity = transform.forward * bulletSpeed * Time.deltaTime;
                        //SoundManager.instance.PlaySingle(fireballSound);
                        MagicUses--;
                        break;

                    case 2:
                        playerMove.JumpForce = 400;
                        MagicUses--;
                        break;
                }
            }
            
            //Stop Animation
            attack = false;
            anim.SetBool("attack", attack);

        }

Edit: Link to image below

Well I figured it out. The code was attached to another gameObject (intentional) and was trying to access the animator of that object. Once I declared a game object that was holding the player and an animator that called the get component of the player, it worked fine!

// Variables
Public GameObject player;
Animator otherAnim;

void Awake()
{
     otherAnim = player.GetComponent<Animator>();
}