- Home /
Can you not use while loops in unity? if so how can i change this to work,Can you not use while loops in unity?
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.AI;
public class EnemyAI : MonoBehaviour { private int x; private int y = 0; private int z; private bool reachedDestination = false;
NavMeshAgent navMeshAgent;
Animation animations;
private Transform destination;
Vector3 position;
// Start is called before the first frame update
void Start()
{
navMeshAgent = GetComponent<NavMeshAgent>();
animations = GetComponent<Animation>();
if(navMeshAgent == null)
{
Debug.LogError("Nav Mesh Agent not found");
}
controlEnemy();
}
void controlEnemy()
{
while (true)
{
x = Random.Range(-20, 20);
z = Random.Range(-20, 20);
setDestination(new Vector3(x, y, z));
if (Input.GetButtonDown("cancel")){
continue;
}
}
}
void setDestination(Vector3 targetDestination)
{
if (navMeshAgent != null)
{
navMeshAgent.SetDestination(targetDestination);
animations.Play("Walk");
}
while (!reachedDestination)
{
if(gameObject.transform.position == targetDestination)
{
reachedDestination = true;
}
}
}
} ,using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.AI;
public class EnemyAI : MonoBehaviour { private int x; private int y = 0; private int z; private bool reachedDestination = false;
NavMeshAgent navMeshAgent;
Animation animations;
private Transform destination;
Vector3 position;
// Start is called before the first frame update
void Start()
{
navMeshAgent = GetComponent<NavMeshAgent>();
animations = GetComponent<Animation>();
if(navMeshAgent == null)
{
Debug.LogError("Nav Mesh Agent not found");
}
controlEnemy();
}
void controlEnemy()
{
while (true)
{
x = Random.Range(-20, 20);
z = Random.Range(-20, 20);
setDestination(new Vector3(x, y, z));
if (Input.GetButtonDown("cancel")){
continue;
}
}
}
void setDestination(Vector3 targetDestination)
{
if (navMeshAgent != null)
{
navMeshAgent.SetDestination(targetDestination);
animations.Play("Walk");
}
while (!reachedDestination)
{
if(gameObject.transform.position == targetDestination)
{
reachedDestination = true;
}
}
}
}
Answer by davidcox70 · Aug 11, 2020 at 10:24 AM
While statements do work, but you have to be mindful of how unity works. For the most part, your script executes between frame updates. If you make a loop in your script, then the next frame will not be delivered until all of your script (including loops) have been executed. So if you have a "while (true)" section, there will be no output from Unity until that loop is broken. Look into the Update() built in function and Co-Routines.
Answer by IllegalRazer · Aug 11, 2020 at 10:44 AM
To avoid trapping the thread in your while loop, you can use a coroutine:
IEnumerator setDestination(Vector3 targetDestination)
{
if (navMeshAgent != null)
{
navMeshAgent.SetDestination(targetDestination);
animations.Play("Walk");
}
while (!reachedDestination)
{
if(gameObject.transform.position == targetDestination)
{
reachedDestination = true;
}
}
}
and starting the coroutine by: StartCoroutine(setDestination());
You need to call yield return null
before the end of the while
loop
Your answer
![](https://koobas.hobune.stream/wayback/20220613005305im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
[Closed]Unity crash, while/for loop 3 Answers
Can I use a while() in the function OnDrawGizmos() ? 1 Answer
Rotate object as long as it's colliding 0 Answers
XmlTextReader problem 1 Answer
Coroutine "While" Setup 1 Answer