Timer stopped on gameover in car racing game unity

Hello everyone,

I have made a simple car racing game using keyboard input…In my game when game is over my car is stopped but timer is still working…I want to stop the timer when car stop…but I don’t want to use Time.timeScale function bcoz it stopped whole game…please help me …i will really appreciate your time taken to preview this post…:slight_smile:

thanks in advanced…
Enjoy…:slight_smile: :slight_smile:

Enclosed your timer into an if statement:

if(running)//timer logic

if(carAtLastCheckpoint)running=false;

Considering your timer goes like this:

var running:boolean=true;

var timer:float;

function Update(){
  if(running)timer += Time.deltaTime;
  if(Input.GetKeyDown(KeyCode.Space))running = !running;
  print(timer);
}

The program above will print the time as it goes and if you press space the timer stops. Press again the timer goes again.

In your game you would have a endOfRace boolean with this below attach to the last checkpoint with a trigger box:

function OnTriggerEnter(other:Collider){
    if(other.gameObject.tag == "Car")endOfRace = true;
}

The little example program uses Input for you to try, what you would need instead in your game is:

if(endOfRace){ 
   running = false;
   endOfRace=false;
 }

Any questions, go ahead as a comment.

Hi,

I used startTime= Time.time;
but what should i do to stop my timer…and i did like this only but timer is not stopped on game over…thanx for revert me back…:slight_smile:
like below:-

if(startGame==true)
{
var guiTime = Time.time - startTime;

var minutes : int = guiTime / 60;
var seconds : int = guiTime % 60;
var fraction : int = (guiTime * 100) % 100;

text = String.Format (“{0:00}:{1:00}:{2:000}”, minutes, seconds, fraction);
GUI.Label (Rect (1220, 50, 100, 30), text);
}
when i do startgame==false then also my timer is working…

Pragya