- Home /
Implementing a Lap/Checkpoint System (Racing Game)
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...
You aren't calling out the line where the error occurs, and its impossible for us to accurately identify line 13 in your code due to possible truncation of the script by not copying/pasting the entire thing. Please highlight (via a comment) in your script where the error occurs.
That being said, however, I believe that PlayerTransform is null. It also might be better in this case, because it won't be happening every frame, to not cache the reference on Start, but rather grab the script via GetComponent during the collision with the trigger collider. Doing this will also remove the need to check the tag due to checking for the component ins$$anonymous$$d.
Line 13 is correct and fully there as it is inside my script inside Unity, it doesn't really say where the error is it just points me to that entire line
Sorry i am a bit new to all this
Yeah, sure, but I was saying that we can't know with absolute certainty which line that is unless you point it out. So, again, add a comment or somethig on the line with the error, because without knowing which line has the error we can offer no help.
To try and track down the problem on your own try using the debugger or (if it crashes for you) some Debug.Log statements.
break up your multiple dereferences everywhere into individual lines of code doing one at a time then this will become trivial to debug. assu$$anonymous$$g line 13 there is actually line 13 in your file it is a huge chain of derefernceing - any part of it could be failing.
transform == playerTransform.GetComponent(CarCheckpoint).checkPointArray[CarCheckpoint.currentCheckpoint].transform
get the car checkpoint component and put it in a variable, then the checkpoint array, then current checkpoint, then fetch the checkpoint out of the array into another variable, then put the transform in a variable so that the final if statement looks something like.
if( transform == otherTransform )
as a rule of thumb multiple dereferences on a single line of code are bad practice in every program$$anonymous$$g language - from C through to JavaScript embedded in Unity.
I changed your answer to a comment. Ideally you'd now post you solution as the answer and accept it.
Answer by Nomibuilder · Apr 08, 2015 at 07:17 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
When i tried using the Checkpoint script i keep getting an error saying that laps does not exist in the current context, what do i reference to fix this
Answer by Gigabeard · May 02, 2014 at 02:58 PM
Sorry Guys in the end i scrapped this feature from my game, but the help was appreciated
Quite simply, I would attempt a frivolous guess and say you have no collider attached to either the player or either one/some/all of your checkpoint objects. read your debugger dude... You have a ..
NullReferenceException:
...which means the..
(in this case a component of type Collider) ...was found ....Object reference
not set to an instance of an object
(where you said to look in the code, well... we looked and there was no Collider attached so we cant reference the Collider )
For stupids like me: your component that was needed to engage in the trigger events you were asking to be handled cannot complete because at least one component(an isTrigger checked collider) was not found attached to the objects in question (you player object and/or your checkpoint objects) so it cannot execute your code correctly)
Here it just telling you where to find the faux pas including the object currently being found as null(other). As a wild guess id say your checkpoint object either have no collider or they are not set to "isTrigger" and where the faux pas resides...
Checkpoints.OnTriggerEnter (UnityEngine.Collider other) (at Assets/Scripts/Checkpoints.js:13)
Anyway, Don`t give up and remove features because they are too hard etc, what if you created the next amazing mechanic in ga$$anonymous$$g but you didn`t cos it was harder than you!
Take care bud and with $$anonymous$$e and everyone else comments here, it should be pretty obvious to you by now what is wrong with your code. However, if you`ve read this and still find yourself stuck, set up a little package with your scene exactly as you have with the problem in including any the relevant checkpoints and player objects and their code... with this I can recreate your scenario here and most likely resolve it for you. Everyone else is correct though, without your full code or the chunk that actually is causing you problems, line 13 could be any line couldn`t it?!
$$anonymous$$y limited experience tells me what has been discussed above is your first port of call. I fall else fails, send up a zipped package and i will take a look as i`m sure others will. Try not to give up on your ideas and concepts dude, don`t let it defeat you, you might be the next Peter $$anonymous$$olyneux or Notch Take care bud Gruffy ;)
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Individual racers passing checkpoints racing game unity 0 Answers
RepeatButton Problem 1 Answer
Reset function for AI car? 0 Answers