- Home /
NullReferenceException: Object reference not set to an instance of an object
I am trying to make an enemy movement script for my tower defence game but i keep getting this error and my enemy is not moving
NullReferenceException: Object reference not set to an instance of an object EnemyMovement.Update () (at Assets/Scripts/EnemyMovement.cs:16)
Here is my script
public float speed = 10f;
private Transform target;
private int wavepointIndex = 0;
void start() {
target = Waypoints.points[0];
}
void Update() {
Vector3 dir = target.position - transform.position;
transform.Translate (dir.normalized * speed * Time.deltaTime, Space.World);
if (Vector3.Distance (transform.position, target.position) <= 0.4f) {
GetNextWaypoint();
}
}
void GetNextWaypoint() {
if (wavepointIndex >= Waypoints.points.Length -1) {
Destroy (gameObject);
return;
}
wavepointIndex++;
target = Waypoints.points [wavepointIndex];
}
If any one knows what the problem is your help is greatly appropriated
thanks
Are you sure that target is not null? Could you place a if(target == null){ Debug.Log("is null"); }
In your update just before the Vector3 dir creation?
Your answer
Follow this Question
Related Questions
The file MemoryStream is corrupted! Remove it and launch unity again 6 Answers
Remote Config gives me a NullReferenceException error 0 Answers
Using rigidbody for collisions only, not movements? 1 Answer
Fixing this Null Reference Exception? 1 Answer
Using reflection, sometimes a field is "null" and sometimes it is "Null"? 3 Answers