Hi
I make 3d racing game and I have some problems with checkpoints system. I found this tutorial Checkpoint and Lap System in Unity Engine - YouTube.
I have problem with using script in my 3d game.
If I play game, unity show following error:
“NullReferenceException: Object reference not set to an instance of an object
checkpoints.OnTriggerEnter (UnityEngine.Collider other) (at Assets/Standard Assets/Scripts/checkpoints.js:14)”
I used scripts: CarCheckpoint.js and Checkpoints.js from tutorial. Files can be download from tutorial Checkpoint and Lap System in Unity Engine - YouTube
Where should I set “3d text”? To Camera or MainCamera?
If you are using C# Language. Then here is the C# version of CheckPoint and Laps system.
Laps Script
using UnityEngine;
using System.Collections;
public class Laps : MonoBehaviour {
// These Static Variables are accessed in "checkpoint" Script
public Transform[] checkPointArray;
public static Transform[] checkpointA;
public static int currentCheckpoint = 0;
public static int currentLap = 0;
public Vector3 startPos;
public int Lap;
void Start ()
{
startPos = transform.position;
currentCheckpoint = 0;
currentLap = 0;
}
void Update ()
{
Lap = currentLap;
checkpointA = checkPointArray;
}
}
Check Point Script
using UnityEngine;
using System.Collections;
public class checkpoint : MonoBehaviour {
void Start ()
{
}
void OnTriggerEnter ( Collider other )
{
//Is it the Player who enters the collider?
if (!other.CompareTag("Player"))
return; //If it's not the player dont continue
if (transform == Laps.checkpointA[Laps.currentCheckpoint].transform)
{
//Check so we dont exceed our checkpoint quantity
if (Laps.currentCheckpoint + 1 < Laps.checkpointA.Length)
{
//Add to currentLap if currentCheckpoint is 0
if(Laps.currentCheckpoint == 0)
Laps.currentLap++;
Laps.currentCheckpoint++;
}
else
{
//If we dont have any Checkpoints left, go back to 0
Laps.currentCheckpoint = 0;
}
}
}
}
And Just Follow this tutorial to attach scripts on your Game Objects. 1
I found where I make mistake. I deleted from code this fragment in checkpoint.js:
//Update the 3dtext
Camera.main.GetComponentInChildren(TextMesh).text = "Checkpoint: “+(CarCheckpoint.currentCheckpoint)+” Lap: "+(CarCheckpoint.currentLap);
and I added this:
function OnGUI () {
GUI.Label (Rect (10, 10, 200, 20), "Checkpoint: “+(CarCheckpoint.currentCheckpoint)+” Lap: "+(CarCheckpoint.currentLap));
}
Sorry for unnecessary question.