Hello! So I am just starting to use C# on Unity. I am creating a simple game and I am done with Positions, Movement, etc. But after some hours of coding and adding more to the game, I suddenly got the “NullReferenceException” Error. I looked on other threads and had answers but not like mine. I am having trouble with this and I am confused at the same time. My Error says:
NullReferenceException: Object reference not set to an instance of an object
Patrol.Update () (at Assets/Scripts/Patrol.cs:18)
I am hoping someone can fix it for me! Here is my code with the error that i’m confused with.
using UnityEngine;
using System.Collections;
public class Patrol : MonoBehaviour {
public Transform[] patrolPoints;
public float moveSpeed;
private int currentPoint;
// Use this for initialization
void Start () {
transform.position = patrolPoints[0].position;
currentPoint = 0;
}
// Update is called once per frame
void Update () {
if (transform.position == patrolPoints [currentPoint].position)
{
currentPoint++;
}
if (currentPoint >= patrolPoints.Length)
{
currentPoint = 0;
}
transform.position = Vector3.MoveTowards (transform.position, patrolPoints[currentPoint].position, moveSpeed * Time.deltaTime);
}
}