- Home /
AI script making unity no longer respond.
So, I have an AI script that is unfinnished, and I am currently trying to test to see if a force of ten is applyed to it.
But currently the whole script seems to make unity no longer respond.
So, I changed a method with a while loop inside it to a coroutine, (By this point I have two coroutines with while loops)
The issue persisted.
so here is the code in question, its the whole script just incase the issue is being caused by another bit of the script entirely.
So anyone know the issue? I am fairly new so it may be an obvious issue that I just missed.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
using UnityEngine.Audio;
//AI
public class EstherchildAI : MonoBehaviour
{
public GameObject sight;
public GameObject Estherchild;
public GameObject Player;
private Rigidbody2D EsthyRigi;
public float speed = 4;
public string DirectionToMove;
public enum State
{
Lurking,
Chasing
}
public State state;
private bool alive;
bool playerDetected = false;
private void OnTriggerEnter2D(Collider2D coll)
{
if (coll.tag == "player")
{
PlayerHasBeenDetected();
state = EstherchildAI.State.Chasing;
}
}
void PlayerHasBeenDetected ()
{
playerDetected = true;
}
private void Start()
{
EsthyRigi = Estherchild.GetComponent<Rigidbody2D>();
state = EstherchildAI.State.Lurking;
alive = true;
StartCoroutine("FSM");
}
IEnumerator FSM()
{
while (alive)
{
switch (state)
{
case State.Lurking:
StartCoroutine (Lurking(DirectionToMove));
break;
case State.Chasing:
Chasing();
break;
}
}
yield return null;
}
IEnumerator Lurking ( string Direction)
{
while (alive)
{
switch (Direction)
{
case "Right":
break;
case "Left":
EsthyRigi.AddForce(transform.forward * speed);
break;
}
}
yield return null;
}
void Chasing ()
{
}
}
Answer by $$anonymous$$ · Jul 04, 2018 at 02:42 AM
Managed to solve it on my own.
First line I put in every while loop inside a coroutine was yield return new WaitForSeconds(0.1f);
doing this seems to have fixed it!
Your answer
Follow this Question
Related Questions
OnTrigger Stay/Exit - Stay Overriding Exit 2 Answers
Update SetDestination only every 0.2s with coroutines ? 5 Answers
AI Coroutine or ScriptableObject ? 1 Answer
Character AI in ScriptableObjects? 1 Answer
How to start at a specific coroutine? 0 Answers