- Home /
Simple checpoint system in 3d racing game - some issues.
Hi
I make 3d racing game and I have some problems with checkpoints system. I found this tutorial http://www.youtube.com/watch?v=7NehsLWcFIU.
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 http://www.youtube.com/watch?v=7NehsLWcFIU
Where should I set "3d text"? To Camera or MainCamera?
Answer by Nomibuilder · Apr 08, 2015 at 07:20 AM
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. Link
Hello Nomibuilder, I have the same script and I am having an issue with the static variables, I hope you can help me to solve this problem please. http://answers.unity3d.com/questions/1015525/static-variables-issue.html
Answer by kapuscianyglab · Sep 30, 2013 at 08:40 PM
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.
Your answer
![](https://koobas.hobune.stream/wayback/20220613115105im_/https://answers.unity.com/themes/thub/images/avi.jpg)