Game Timer help needed

Attempting to make a script that runs a timer that keeps track of how long you survived in game, and also updates the highscore if the timer is larger than it. This all works perfectly right now and i have achieved the above, but now I’ve been trying to have the game and timer reset when you lose, the only problem is that the timer just pauses and then upon reset carries on from where it left off. I need to set the value of the timer to zero but this doesn’t seem to work, any help??

For the GUI to display the Timer

public class Timer : MonoBehaviour {
 
//Reference for timer layout
public int minutes;
public int seconds;
public int fraction;	

//Timer
public static float countTime;

//bool "go" is available to use by other scripts also
public static bool go;
 
    void Start(){
	
    go = true;
	
	}
 
    void Update () 
	{

    //Only happen if go = true 
	//Triggers and displays the time with correct format upon go = true
    if(go) {

	  countTime = Time.time ;
	  minutes = (int)(countTime/60f );
	  seconds = (int)(countTime % 60f);
	  fraction =  (int)((countTime *10) %10);
	  guiText.text = string.Format("{0}   .   {1}   .   {2}", minutes, seconds, fraction);
	  }
	  
	}
}

For a seperate GUI to display a highscore as explained above

public class Bestscore : MonoBehaviour {

//For best time
public int minutesB;
public int secondsB;
public int fractionB;

//Defines a public float “bestTime”
public float bestTime = 0f;

void Start() {

//On startup PlayerPrefs are loaded
bestTime = PlayerPrefs.GetFloat("bestTime", 0f);

} 

//At the end of the game run below
void OnDestroy() {

//Saves the data for later use
PlayerPrefs.SetFloat("bestTime", bestTime);
}

void Update() {

//If score is higher than the high score the timer will match the score
if(Timer.countTime > bestTime) {
  bestTime = Timer.countTime;

  }
}
  
void FixedUpdate() {

//Layout for high score GUI text 
minutesB = (int)(bestTime/60f );
secondsB = (int)(bestTime % 60f);
fractionB =  (int)((bestTime *10) %10);
guiText.text = string.Format("BEST TIME    :    {0}   .   {1}   .   {2}", minutesB, secondsB, fractionB); 	  
}

}

I’ve tried adding countTime = 0f; to the start but that dosen’t do anything.
Sorry if this is too long or troublesome, any help would be appreciated and i’ll be on in the evening to check back in.

What I would do is this:

  1. use a variable to decide when the timer has been started

ie.

//when button is clicked or whatever set timerStarted to true
if(timerStarted)
{
   timeOfStart = Time.time;
   timerStarted = false;
}

if(IAmTiming)
{
   float currentTime = Time.time - timeOfStart;

   minuites = (int)(currentTime/60);
   //etc
}

Then each time you want to reset the timer set timerStarted to true

Feel free to ask for more detail or prove me wrong.

Minchuilla

Hey,

Instead of using Time.time increment countTime by Time.deltaTime;

countTime += Time.deltaTime;

You can then simply do countTime = 0; to rest the timer, Time.time is the seconds since the game started you don’t want to use that here.

Phill