Javascript Basic Lap Timer with GUI

I have been searching the forums for literally days, and can’t find a working answer. I have tried dozens of possible solutions but nothing is working for me.
Basically, I have a basic racing game. I have a nice little welcome screen, with a GUI that loads the map with the track. I got a countdown timer to say 3, 2, 1, GO! But now I’m trying to figure out how to make a lap timer. I only need the car to do one lap, and then it uploads the timer to a server I have. I have tried using triggers to start and stop the time, but nothing seems to work. I would post code, but there are just so many versions I have tried. If anyone knows of any good scripts in the wiki, or tutorials on the internet, literally anything, it would be greatly appreciated.

You’re welcome to use mine. It’s a little more complex and probably a little rough as I’m a bit of a noob but it does the job. It takes into account multiple laps and displays the last lap etc etc and can also detect if the lap was legitimate (though that needs tweaking). Take from it what you want :wink:

// Public Variables
var ValidLap : GameObject;
var LapCounter : GameObject;
var LapCounterText : GUIText;
var LapCounterTextShadow : GUIText;
var iStartSequenceTime : int = 9;
var LapTimeText : GUIText;
var LapTimeTextShadow : GUIText;
var LastLapText : GUIText;		// Displays the previous lap time in the centre of the screen for the player
var LastLapTextShadow  : GUIText;
var iNumberOfLaps : int = 5;
var fAllLapTimes : float[] = new float[iNumberOfLaps]; // An array to store all laptimes.


// Private Variables
private var bValidLapIsTrue = false;
private var iCurrentLap : int = 1;
private var iMinutes : int;
private var iSeconds : int;
private var iHundredths : int;
private var iMinutes2 : int;
private var iSeconds2 : int;
private var iHundredths2 : int;
private var fCurrentLapTime : float;
private var fPreviousLap : float;		// Float to store the previous completed lap
private var fLapTimeTotal : float;		// Container to store all previous laptimes together.
private var bSetLapTime = false;
private var bCurrentLapReset = false;



function OnTriggerEnter(other : Collider)
{
	if(other == ValidLap.collider)
	{
		if(bValidLapIsTrue)
		{
			bValidLapIsTrue = false;
		}
		else
		{
			bValidLapIsTrue = true;
		}
	}	
	
	if(other == LapCounter.collider && bValidLapIsTrue)
	{
		++iCurrentLap;
		bValidLapIsTrue = false;
		bSetLapTime = true;
	}	
}


function Update()
{
	// Update the onscreen lap counter
	LapCounterText.text = ("LAP " + (iCurrentLap));
	LapCounterTextShadow.text = ("LAP " + (iCurrentLap));
	
	if (LevelSetup.g_bGameInPlay)
	{
		if (!bCurrentLapReset)
		{
			fCurrentLapTime = Time.time - iStartSequenceTime; // Account for the starting sequence delay
		}
		else
		{
			// Reset the current lap timer, accounting for original start seq delay
			fCurrentLapTime = ((Time.time - fLapTimeTotal) - iStartSequenceTime);
		}
	}
	else
	{
		// Keeps timer at zero until game underway
		fCurrentLapTime = 0.0f;
	}
	
	// Lap timer
	iMinutes = (fCurrentLapTime /60f);
	iSeconds = (fCurrentLapTime  % 60f);
	iHundredths = ((fCurrentLapTime * 10) %10);
	LapTimeText.text = String.Format("{0:00}:{1:00}:{2:00}",iMinutes,iSeconds,iHundredths);
	LapTimeTextShadow.text = String.Format("{0:00}:{1:00}:{2:00}",iMinutes,iSeconds,iHundredths);
	

	// Set the previous lap time
	while (bSetLapTime)
	{
		fPreviousLap = fCurrentLapTime;
		print(fPreviousLap);
		bSetLapTime = false;
		fCurrentLapTime = 0.0f; // Reset the current Laptime
		bCurrentLapReset = true;
		fLapTimeTotal += fPreviousLap; // Adds to the total race time
		DisplayLapTime();
		
		if (iCurrentLap <= fAllLapTimes.Length) // So array doesn't go out of bounds
		{
			for (var i = 0; i < iCurrentLap; ++i)
			{
				fAllLapTimes *= fPreviousLap; // Populate the array with lap times*

_ print("Lap: " + fAllLapTimes*);_
_
}_
_
}_
_
}_
_
}*_

function DisplayLapTime()
{
* var LastLapTextCopy = Instantiate(LastLapText);*
* var LastLapTextShadowCopy = Instantiate(LastLapTextShadow);*

* iMinutes2 = (fPreviousLap /60f);*
* iSeconds2 = (fPreviousLap % 60f);*
_ iHundredths2 = ((fPreviousLap * 10) %10);_

* for (var i = 0; i < 5; ++i)*
* {*
* LastLapTextCopy.text = (“LAST LAP:
" + String.Format(”{0:00}:{1:00}:{2:00}",iMinutes2,iSeconds2,iHundredths2));*

* LastLapTextShadowCopy.text = (“LAST LAP:
" + String.Format(”{0:00}:{1:00}:{2:00}",iMinutes2,iSeconds2,iHundredths2));*

* yield WaitForSeconds(0.5);*

* LastLapTextCopy.text = (" ");*
* LastLapTextShadowCopy.text = (" ");*
* yield WaitForSeconds(0.5);*
* }*

* yield WaitForSeconds(3.0f);*
* Destroy(LastLapTextCopy);*
* Destroy(LastLapTextShadowCopy);*
}

At the beginning of the race (just after the countdown):

this.startTime = Time.time; // startTime is a float member of your script

At the end:

float fTime = Time.time - startTime; // absolute value in seconds
int nHundredths = (int)(Mathf.Floor(fTime*100))%100;
int nSecond = (int)Mathf.Floor(fTime);
int nMin = (int) nSecond/60;
nSecond = nSecond%60;
string sText = string.Format("{0:00}:{1:00}.{2:00}", nMin, nSecond, nHundredths);