Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Gigabeard · Jan 11, 2014 at 11:35 PM · racing gamecheckpoints

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...

Comment
Add comment · Show 5
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image iwaldrop · Jan 12, 2014 at 12:03 AM 0
Share

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.

avatar image Gigabeard · Jan 12, 2014 at 02:25 PM 0
Share

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

avatar image iwaldrop · Jan 12, 2014 at 06:52 PM 0
Share

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.

avatar image semiessessi · Jan 12, 2014 at 07:16 PM 2
Share

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.

avatar image iwaldrop · Jan 13, 2014 at 04:36 AM 0
Share

I changed your answer to a comment. Ideally you'd now post you solution as the answer and accept it.

2 Replies

· Add your reply
  • Sort: 
avatar image
4

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

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Finjago06 · Jul 05, 2020 at 12:44 PM 0
Share

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

avatar image
-1

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

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Gruffy · May 02, 2014 at 04:05 PM 0
Share

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..

Object reference

(in this case a component of type Collider) ...was found ....

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

23 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

How to import the object from server to unity 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges