Currently I am trying to get my current object that contains a NavMeshAgent to move towards 3 different waypoints. When one waypoint is reached, the object will go to the next waypoint. I am receiving an error on the line that contains > agent.SetDestination (wayPoint [0].position);
. Before this line I have already set my agent variable to the component attached to my current object so I am a little confused why this null reference exception is occurring. Thanks in advance.
using UnityEngine;
using System.Collections;
public class NPCmovement : MonoBehaviour {
public GameObject player;
public Transform[] wayPoint = new Transform[3];
Vector3 dist;
NavMeshAgent agent;
// Use this for initialization
void Start () {
agent = GetComponent<NavMeshAgent> ();
}
void Update ()
{
agent.SetDestination (wayPoint [0].position);
if (transform.position == wayPoint [0].position) {
agent.SetDestination (wayPoint [1].position);
//if(agent
if (transform.position == wayPoint [1].position) {
agent.SetDestination (wayPoint [2].position);
}
}
}