Coroutine does not work.

I have an enemy that charges at the player in my 2d platformer. I am using a co-routine attack() so that when the player walks into his range he waits for 0.5 seconds and then charges. However even after he starts waiting (WaitForSeconds) if the character jumps out of his range he will stop waiting and execute his main loop in FixedUpdate() and will walk, track the player, and other stuff even though anim.GetBool(“attacking”) == true.
For some reason the animator variable attacking goes to false if the character jumps out of range even though I don’t want it too. The only time I ever set attacking to false is at the end of attack(). Please help. Thank YOU
Here is the code:

public class EnemyChargerScript : MonoBehaviour {
	bool facingRight = true;
	public LayerMask whatIsGround;
	public LayerMask whatIsPlayer;
	float groundRadius = 0.2f;
	float attackRadius = 0.2f;
	public Transform GroundCheck;
	public Transform PlayerCheck2;
	public bool grounded = false;
	public bool Dead = false;
	public Transform onEdgeCheck;
	public bool onEdge;
	public Transform PlayerCheck;
	public float originalmove;
	float newmove;
	Vector3 BackupPosition;
	Animator anim;
	public GameObject Player;
	public bool dead;
	public PlayerControl playerscript;
	public GameWideScript gamescript;
	public bool activated = false;
	Vector3 inview;
	bool pushed = false;
	public int chargeSpeed;






	
	// Use this for initialization
	void Start () {		

		anim = GetComponent<Animator> ();
		anim.SetBool ("Attacking",false);



	}

	// Update is called once per frame
	void FixedUpdate () {
		grounded = Physics2D.OverlapCircle (GroundCheck.position, groundRadius, whatIsGround);
		onEdge = Physics2D.OverlapCircle (onEdgeCheck.position, groundRadius, whatIsGround);
		if (!grounded){
			this.rigidbody2D.velocity = new Vector2(0,0);
			this.transform.position = BackupPosition;
		}

		if (!onEdge) {
			BackupPosition = this.transform.position;
				}
		if (!activated) {
						inview = Camera.main.WorldToViewportPoint (this.transform.position);
			if (inview.x <= 1 && inview.x >= 0 && !activated) {
				activated = true;
			}
				}


		if(!anim.GetBool("Dead") && playerscript.Dead == false && !anim.GetBool("Attacking")  && activated)
		{
		if(Physics2D.OverlapArea (PlayerCheck.position,PlayerCheck2.position, whatIsPlayer, -20, 20))
		        Attack ();

		
		        


		if (this.transform.position.x < Player.transform.position.x){
						newmove = originalmove;

			}
		if (this.transform.position.x > Player.transform.position.x){
						newmove = originalmove * -1;

			}

		rigidbody2D.velocity = new Vector2 (newmove, rigidbody2D.velocity.y);

		anim.SetFloat("Speed",Mathf.Abs(newmove));
		if (newmove < 0 && !facingRight)
			Flip ();
		
		if (newmove > 0 && facingRight)
			Flip ();


		

		




		BackupPosition = this.transform.position;
			
		}
		if (playerscript.Dead == true) {
						this.rigidbody2D.velocity = new Vector2 (0, 0);
			anim.SetBool("Pdead", true);
				}
		
	}
	void Flip()
	{
		facingRight = !facingRight;
		Vector3 theScale = transform.localScale;
		theScale.x *= -1;
		transform.localScale = theScale;
	}

	void Attack()
	{
		Debug.Log ("enemyAttack");
		anim.SetBool ("Attacking",true);
		rigidbody2D.velocity = new Vector2 (0, 0);
		StartCoroutine("attack");


	}
	public void Die()
	{
		Debug.Log ("enemyDie");
		this.rigidbody2D.velocity = new Vector2 (0, 0);
		anim.SetBool ("Dead", true);
		Dead = true;

	}

	public IEnumerator attack(){



		yield return new WaitForSeconds (0.5f);
		Debug.Log ("kill");
		if (facingRight) {
						rigidbody2D.AddForce (new Vector2 (chargeSpeed * -1, 0));
				}
		else {
			rigidbody2D.AddForce(new Vector2 (chargeSpeed, 0));
				}
		Debug.Log ("x");
		anim.SetBool ("Attacking", false);

	}

	void OnCollisionEnter2D(Collision2D collision){
		if (collision.gameObject.name == "character" && anim.GetBool("attacking") == true) {
						playerscript.Die ();
				}
				
	}
	public void x(){

	}



}

your coroutine is called every time the enemy is in range but you havn’t put any condition to stop it. I think you should check once after the waitforsecond(), if the same condition holds for what you called the attack coroutine. If the condition is false you should use STOPCOROUTINE function to stop the execution of lines next to it