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.
Try adding a yield return null;
outside the while. Also, it would be easier if you put your code as text and not as an image.
Answer by zion379 · Mar 09, 2018 at 03:44 AM
@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;
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612154058im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Creating an enemy when a random value = 1 2 Answers
A* Pathfinding and keep the enmy at range 0 Answers
Find nearest enemy object in radius 2 Answers
How do i randomize certain waypoints at the end of a path? 0 Answers
How to move model on x axis in increments of 1? Starting from zero at a range of -2 to 2. 2 Answers