Why isn't my enemy pausing in between waypoints? C#

My enemy is using a completely custom AI made using nothing but unity’s default tools. It uses a NavMesh and NavMeshAgents.

I’m having problems where I want my enemy to stop for 4 seconds after each waypoint is reached, but they’re not stopping.

Edit: It also seems that as time goes on the AI starts doing more and more new paths at one time. It starts out working perfect, after 30 seconds it starts doing two path calculations rapidly, a minute, and we’re up to 3.

Here’s my code:

			if (nav.remainingDistance < 0.1)
			{
				nav.Stop();
				nav.ResetPath();
				if(hasMoved && !newDestCalled)
				{
					Debug.Log ("Trying to call New Destination");
					newDestCalled = true;
					StartCoroutine(NewDestination ());

				}
				else
				{
					waypointRandom = Random.Range(0, waypoints.Length);
					Debug.Log (waypointRandom);
					nav.SetDestination(waypoints[waypointRandom].position);
					hasMoved = true;
				}



	IEnumerator NewDestination()
	{
		yield return new WaitForSeconds(4);
		waypointRandom = Random.Range(0, waypoints.Length);
		Debug.Log (waypointRandom);
		nav.SetDestination(waypoints[waypointRandom].position);
		newDestCalled = false;

	}

I’m assuming that your if (nav.remainingDistance < 0.1) bit is called in an Update() loop? If so, there’s a flaw in your logic.

It’s unclear what the ifMoved variable does, but it actually doesn’t matter. Let’s assume that ifMoved is true to start with, and that newDestCalled is false, in which case the first if block gets executed and calls your NewDestination coroutine (which is where you’re assuming a 4 second pause will be introduced).

But, on the very next frame, newDestCalled is now true (since it was set in line 8), which means that the else block now gets executed and immediately sets a new nav destination, without waiting… so there’s never a time when the pause introduced by the coroutine would take be noticeable.