- Home /
Object reference is NULL when IT IS set to an instance of an object?
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);
}
}
}
Answer by nksG · Oct 03, 2016 at 03:58 PM
you can declare the Transform Arrays like this: (from the inspector you can set the size and the values)
public Transform[] waypoint;
to be always sure your waypoints are not null and easily editable. Cheers.
Your answer
Follow this Question
Related Questions
Has my project's data corrupted? Script keeps returning null but 20 minutes ago it wasn't? 1 Answer
Showing unity ads from other script? 1 Answer
NullReferenceException: Object reference not set to an instance of an object ProgressBar.Start () 2 Answers
While level restarted I get NullReferenceException 0 Answers
null reference but component shows up in the inspector. 0 Answers