Hello There 1st time Poster
Im Currently making a Racing Game and trying to implement a Lap/Checkpoint System where you have to go through for example 4 checkpoints to complete one lap, the vehicle will collide with the checkpoint but then i get this error
NullReferenceException: Object reference not set to an instance of an object
Checkpoints.OnTriggerEnter (UnityEngine.Collider other) (at Assets/Scripts/Checkpoints.js:13)
Here is the Script attached to my Vehicle
#pragma strict
var checkPointArray : Transform[]; //Checkpoint GameObjects stored as an array
static var currentCheckpoint : int = 0; //Current checkpoint
static var currentLap : int = 0; //Current lap
static var startPos : Vector3; //Starting position
function Start () {
//Store the starting position of the player
startPos = transform.position;
}
Here is the Script Assigned to all my Checkpoints
static var playerTransform : Transform; //Store the player transform
function Start () {
playerTransform = gameObject.Find("Player").transform; //Set the player transform
}
function OnTriggerEnter (other : Collider) {
//Is it the Player who enters the collider?
if (!other.CompareTag("Player"))
return; //If it's not the player dont continue
//Is this transform equal to the transform of checkpointArrays[currentCheckpoint]?
if (transform == playerTransform.GetComponent(CarCheckpoint).checkPointArray[CarCheckpoint.currentCheckpoint].transform) {
//Check so we dont exceed our checkpoint quantity
if (CarCheckpoint.currentCheckpoint + 1<playerTransform.GetComponent(CarCheckpoint).checkPointArray.length) {
//Add to currentLap if currentCheckpoint is 0
if(CarCheckpoint.currentCheckpoint == 0)
CarCheckpoint.currentLap++;
CarCheckpoint.currentCheckpoint++;
} else {
//If we dont have any Checkpoints left, go back to 0
CarCheckpoint.currentCheckpoint = 0;
}
//Update the 3dtext
GetComponent(GUIText).text = "Checkpoint: "+(CarCheckpoint.currentCheckpoint)+" Lap: "+(CarCheckpoint.currentLap);
}
}
Any help would be much appreciated after a good 8+ hours of pulling my hair out…