Why is my navmesh enemy pathing not working?

So I have created this little script to take an array of transforms and make the enemy walk from point to point in circles.

It successfully adds the transforms to the array without any errors, however my character doesn’t move at all, any ideas anyone?
*
using UnityEngine;
using System.Collections;

public class EnemyPathing : MonoBehaviour {

    [SerializeField]
    Animator anim;

    [SerializeField]
    NavMeshAgent nMA;

    [SerializeField]
    Transform[] pathPoints;

    GameObject path;

    int nr;

    void Awake()
    {
        anim = GetComponent<Animator>();
        nMA = GetComponent<NavMeshAgent>();

        path = GameObject.Find(transform.name + "Path");

        pathPoints = new Transform[path.transform.childCount];
        for (int i = 0; i < path.transform.childCount; i++)
        {
            pathPoints *= path.transform.GetChild(i);*

}

nMA.Resume();
}

void Update ()
{
if (nMA.remainingDistance > nMA.stoppingDistance)
{
nMA.destination = pathPoints[nr].position;

}
else if (nMA.remainingDistance <= nMA.stoppingDistance)
{
if(nr < pathPoints.Length)
{
nr++;
}
else
{
nr = 0;
}
nMA.Resume();
}
}
}
*
Thank you in advance

I managed to find a few problems. First, you never set the initial destination, so your agent never has a distance to test against your if statements. Also, your ‘nr < pathPoints.Length’ needs to be ‘nr < (pathPoints.Length - 1)’, since the index starts at 0, not 1.

Finally, I don’t understand the point of the loop you are using to build your path (although I am pretty new at Unity, so I might be missing something). Right now, you can drag the waypoints directly into the navagent’s inspector to build the path list. Do you want to build the list automatically instead? If so, what are you trying to build the list from, all objects that have ‘path’ in their name?

Anyway, assuming you populate the navagent’s path list manually, the following code works to get the navagent looping through all the path points:

using UnityEngine;
using System.Collections;

public class EnemyPathing : MonoBehaviour
{

    [SerializeField]
    NavMeshAgent nMA;

    [SerializeField]
    Transform[] pathPoints;

    // GameObject path;

    int nr = 0;

    void Awake()
    {
        nMA = GetComponent<NavMeshAgent>();

        //path = GameObject.Find(transform.name + "Path");

        //pathPoints = new Transform[path.transform.childCount];
        //for (int i = 0; i < path.transform.childCount; i++)
        //{
        //    pathPoints *= path.transform.GetChild(i);*

//}
nMA.destination = pathPoints[nr].transform.position;
}

void Update()
{
if (nMA.remainingDistance <= nMA.stoppingDistance)
{
if (nr < (pathPoints.Length - 1))
{
nr++;
nMA.destination = pathPoints[nr].transform.position;

}
else
{
nr = 0;
nMA.destination = pathPoints[nr].transform.position;
}
}
}
}
Hope that helps!