With a list of patrol locations initialized on Start(), we can grab the enemy NavMeshAgent component and set its first destination.
Update EnemyBehavior with the following code and hit Play:
// 1
using UnityEngine.AI;
public class EnemyBehavior : MonoBehaviour
{
public Transform patrolRoute;
public List<Transform> locations;
// 2
private int locationIndex = 0;
// 3
private NavMeshAgent agent;
void Start()
{
// 4
agent = GetComponent<NavMeshAgent>();
InitializePatrolRoute();
// 5
MoveToNextPatrolLocation();
}
void InitializePatrolRoute()
{
// ... No changes needed ...
}
void MoveToNextPatrolLocation()
{
// 6
agent.destination = locations[locationIndex].position;
}
void OnTriggerEnter(Collider other)
{
// ... No changes needed ...
}
void OnTriggerExit(Collider other...