Changing NavMesh Target

Hi. I’m struggling with something that I know the solution is painfully obvious.

I’m trying to create a simple AI that navigates towards different locations / destinations. Here is my code so far.

using UnityEngine;
using System.Collections;

public class Nav : MonoBehaviour
{
public Transform destination;
public Transform destination1;
public Transform destination2;
public Transform destination3;
public Transform destination4;

public NavMeshAgent agent;

void Start()
{

    agent.SetDestination(destination.position);

}

void Update()
{

    if (!agent.pathPending)
    {

        if (agent.remainingDistance <= agent.stoppingDistance)
        {

            if (!agent.hasPath || agent.velocity.sqrMagnitude == 0f)
            {

             // Executes below code if AI has stopped moving//
               agent.SetDestination(destination1.position);
           }

}

}
}
}

How do I make it so I can make my AI go to more destinations than two? I tried to copy and paste that if statement 5 times and just changing the destination variable accordingly but that didn’t work. What happened there was my AI went from Destination 1 - Destination 2 - Destination 3 - Destination 2 - Destination 3 - Destination 2 and so on.

You should store your destinations in an array of Transforms. I would use NavAgent.pathStatus to see if the current is finished. Something like this:

public Transform[] destinations;
public int currentTarget = 0;

void Start() {
    agent.SetDestination(destinations[0]);
}

void Update() {
    if(currentTarget < destinations.Length - 1) {
        if(agent.pathStatus == NavMeshPathStatus.PathComplete) {
            currentTarget++;
            agent.SetDestination(destinations[currentTarget]);
        }
    }
}

This is the code that works for me (make sure that target position/transform will be exactly equal to AI’s destination):

using System.Collections;
using System.Collections.Generic;
using UnityEngine.AI;
using UnityEngine;

public class enemyAI : MonoBehaviour
{
    public Transform[] destinations;
    public int currentTarget = 0;

    private NavMeshAgent agent;

    void Start()
    {
        agent = GetComponent<NavMeshAgent>();
        agent.SetDestination(destinations[0].position);
    }

    void Update()
    {
        // Use this if statement condition if you want your AI to patrol
        if(currentTarget >= destinations.Length - 1)
        {            
            currentTarget = 0;  // reset current target
            agent.SetDestination(destinations[currentTarget].position);
        }

        // this if statement controls the AI to navigate to different destinations
        if (currentTarget < destinations.Length - 1)
        {            
            if (this.transform.position == destinations[currentTarget].position)            
            {
                currentTarget++;                
                agent.SetDestination(destinations[currentTarget].position); // set next target
            }
        }
    }
}