Unity 2D animation issue

Unity 2D animation issue

I have this weird animation bug in Unity that I believe is how Unity handles the frame. I dont know how to show the image so I'll do my best to explain it. So I put a bunch of 2D sprites in a sequential order right and I made a timer in my code to play animation whenever. The weird thing is that it does complete the whole animation but then it goes back to the first frame and ends it. I tried changing the timer to every number possible but it just does the same thing or repeats the animation twice when I increase my timer.
Here's the code :
using UnityEngine;
using System.Collections;

public class PlayerAttack : MonoBehaviour {

private bool attacking = false;//Switches for all attac combo
private bool attacking2 = false;
private bool attacking3 = false;

//attack 1 stats
public float attackTimer = 0f;
public float attackCD = .3f;

public Collider2D attackTrigger;//Referring to our attack collider

Animator anim;

// Use this for initialization
void Start () {
    anim = GetComponent ();
    attackTrigger.enabled = false;//Disable our collider at start
}

// Update is called once per frame
void Update () {
    if (Input.GetKeyDown ("space") && !attacking) 
    {
        attacking = true;//We are attacking
        attackTimer = attackCD;//assign variables

        attackTrigger.enabled = true;//Enabled our trigger collider
    }

    //If we are attacking
    if (attacking) {
        //Check if our timer is greater than zero
        if(attackTimer > 0){
            attackTimer -= Time.deltaTime;//Decrease our timer
        }else
        {
            attacking = false;//We arent attacking anymore
            attackTrigger.enabled = false;
        }
    }
    anim.SetBool ("Attack", attacking);
 }
} 

Solutions/Answers:

Answer 1:

There’s no need to attacking variable. it should work.

public class PlayerAttack : MonoBehaviour {

private bool attacking = false;

public float attackTimer = 0f;
public float attackCD = .3f;

public Collider2D attackTrigger;
Animator anim;

// Use this for initialization
void Start () {
    anim = GetComponent<Animator> ();
    attackTrigger.enabled = false;
    attackTimer = attackCD;//       assign variables
}

// Update is called once per frame
void Update () {
    if (Input.GetKeyDown ("space")) 
    {
         anim.SetBool ("Attack", true);

        attackTrigger.enabled = true;
         if(attackTimer > 0){
            attackTimer -= Time.deltaTime;//Decrease our timer
        }else
        {
            attackTrigger.enabled = false;
             anim.SetBool ("Attack", false);
        }
    }


} 

Answer 2:

I believe what you’re looking for is the animation’s WrapMode. Go to the inspector for the Animation file. Click the menu icon next to the “padlock” icon in the very very top-right corner. Change it to Debug and you should see “Wrap Mode” as highlighted here: Animation Wrap Mode

References