I have a coroutine that keeps crashing unity.

here is the coroutine that looks perfectly fine. but when it goes to check if the player has ran away and stops this current coroutine and starts the next one it crashes. or when i stop this coroutine using yield break it crashes.

@chiker Thanks for your help! and I tried what you said and now when I press play unity will freeze up and crash.

Code :

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

public class AIEnemy : MonoBehaviour {

public enum ENEMY_STATE {PATROL, CHASE, ATTACK};
[SerializeField]
ENEMY_STATE currentState = ENEMY_STATE.PATROL;
public ENEMY_STATE CurrentState {
    get {return currentState;}

    set {
        currentState = value;

        StopAllCoroutines();

        switch(currentState) {
            case ENEMY_STATE.PATROL :
            StartCoroutine(AIPatrol());
            break;

            case ENEMY_STATE.CHASE :
            StartCoroutine(AIChase());
            break;

            case ENEMY_STATE.ATTACK :
            StartCoroutine(AIAttack());
            break;
        }
    }
}

NavMeshAgent thisAgent = null;

public Transform patrolDestination = null;
public Transform playerPosition = null;

NPC_LineOfSight thisLineOfSight = null;

void Awake() {
    thisAgent = GetComponent<NavMeshAgent>();
    thisLineOfSight = GetComponent<NPC_LineOfSight>();
}

void Start() {
    CurrentState = ENEMY_STATE.PATROL;
}

IEnumerator AIPatrol() {
    while(currentState == ENEMY_STATE.PATROL) {

        // set the search for player to strict
        thisLineOfSight.Sensitity = NPC_LineOfSight.SightSensitivity.STRICT;

        // move to patrol position
        thisAgent.Resume();
        thisAgent.SetDestination(patrolDestination.position);

    if(thisLineOfSight.CanSeeTarget){
        thisAgent.Stop();
        CurrentState = ENEMY_STATE.CHASE;
        yield break;
    }

        yield return null;    
    }
    yield return null;
}

IEnumerator AIChase() {
    while(currentState == ENEMY_STATE.CHASE){

        // set loose search
        thisLineOfSight.Sensitity = NPC_LineOfSight.SightSensitivity.LOOSE;

        //Chase to last known sighting
        thisAgent.Resume();
        thisAgent.SetDestination(thisLineOfSight.lastKnownSighting);

        if(thisAgent.remainingDistance <= thisAgent.stoppingDistance) {

            // player has ran away?
            if(!thisLineOfSight.CanSeeTarget) {
                Debug.Log("Player has ran away");
                thisAgent.Stop();
                CurrentState = ENEMY_STATE.PATROL;
                yield break;
            } else if (thisLineOfSight.CanSeeTarget) {
                thisAgent.Stop();
                CurrentState = ENEMY_STATE.ATTACK;
                yield break;
            }
        }

        yield return null;
    }
    yield return null;
}

IEnumerator AIAttack() {
    while(currentState == ENEMY_STATE.ATTACK) {

        thisAgent.Resume();
        thisAgent.SetDestination(playerPosition.position);

        // check if player has ran away
        if(thisAgent.remainingDistance > thisAgent.stoppingDistance) {
            //Go back to chasing state
            thisAgent.Stop();
            CurrentState = ENEMY_STATE.PATROL;
            yield break;
        } 
        if (thisAgent.remainingDistance < thisAgent.stoppingDistance) {
            // Start attacking.
            Debug.Log("Attacking player.");
        }

       yield return null;
    }
     yield return null;
}

}