Navmesh agent stops on random waypoints.

I have an enemy that randomly jumps between predefined waypoints. The enemy movement is made using the navmesh agent component.
Public Transform waypoints;
Public Tranaform waypoint;
Public int waypointIndex;

void Update() {
waypoint = waypoints[waypointIndex];

nma.SetDestination(waypoint.position)
}
void OnTriggerEnter (collider other) {
if (other.gameObject.tag == "waypoint")
{
waypointIndex = Random.Range(0,waypoints.length);
}
}

The problem is that sometimes the enemy randomly stops on certain waypoints. I have a feeling it has something to do with the fact that random range is calling the number that the waypoint is already on. Any ideas for making the random range number not calling the same number twice in a row. Or any other solution that I might be missing?

Ps. Sorry if the code is a little messed I’m writing it from memory on my phone.

It stops because you are not checking if randomed waypoint IS SAME WAYPOINT YOU ARE CURRENTLY AT! In that case your OnTriggerEnter won’t run and whole nma stops. Just check if new waypointIndex is != current, and everything will be okey :wink:

Code:

void OnTriggerEnter (collider other) { 
     if (other.gameObject.tag == "waypoint") 
     { 
          int tmpRand = waypointIndex;
          while(tmpRand == waypointIndex) 
          { 
               tmpRand = Random.Range(0,waypoints.length); 
          } 
          waypointIndex = tmpRand; 
     } 
}