Record time on car racing game

Hi im currently using the car racing tutorial to make my own game. I have 6 checkpoints around the track and a finish line. You start off with 30seconds on a countdown timer and each time you get to a checkpoint, time is added to the timer.
Im wanting to implement some sort of leaderboard function to make the game competitive.
How do I display and save the amount of time a player has left on the timer?
Thankyou

There’s a couple of options here.

EZ game saver from Above and Beyond software

or

PlayerPrefs Unity - Scripting API: PlayerPrefs

Both should do the job.

Im sorry i dont understand the text on the unity site and i do not want to use any external software. Could you help me further please

What you can do is a little trick to calculate it so you don’t have to add up time every frame.

Using this function of the Time class you can effectively figure out the time the player has been racing.

Time.timeSinceLevelLoad gives you the time in seconds since the level has loaded. So you will need a variable to save this value when the race starts. Then again when it ends and subtract the first one from the second one.

//Race is starting
Var startTime = Time.timeSinceLevelLoad;
Var endTime;

//Part of lap completed code
if(laps=totalToWin)
{
  endTime = Time.timeSinceLevelLoad;

  var totalTime = endTime-startTime;
}

If the player pauses the game you just need to keep track of when this was and do some more math to figure out how many seconds the game was paused, just like the pseudo code above does total race time.
Hope this helps! :slight_smile:

thankyou, what would be the best way to output this time?

Well I am assuming you are going to display it on the GUI, so I would use GUI.Text to display it. Check out the GUI documentation for more details on how you can use it. Since timeSinceLevelLoad is in seconds you may want to do some math to have it calculate the seconds into minutes and seconds. If you want the time to be on screen constantly to let the user know how much time has passed then you should actually add up all the times between frames as you want that time updated on your GUI each frame. Then its just a simple time=0. then every frame the race is running time+=time.deltaTime.

i get all of these errors

352523--12251--$screen_shot_2010_07_23_at_145040_395.png

//Race is starting
Var startTime = Time.timeSinceLevelLoad;
Var endTime;

//Part of lap completed code
if(CarLap==1)
{
endTime = Time.timeSinceLevelLoad;

var totalTime = endTime-startTime;
}

var intTime : int;
var minutes : int;
var seconds : int;
var fraction : int;

function FormatTime (time) {
intTime = time;
minutes = intTime / 60;
seconds = intTime % 60;
fraction = time * 10;
fraction = fraction % 10;

//Build string according to format
//12(minutes):34(seconds).5(fraction)

timeText = minutes.ToString () + “:”;
timeText = timeText + seconds.ToString ();
timeText += “.” + fraction.ToString ();
return timeText;
}

this is my code after ive changed it

hi i wanted a help i made a 2d car racing game . my game starts automatically and only on getting hit by another car my game gets over and replay option is invoked . so i want to add my timer accordinly that automatically it starts and on hit the timer stops . the time should be in minutes and seconds form could anyone please help me with the code according to my game