x


How to reset timer? (C#)

I have a timer script that counts down and I would like the timer to reset when the user clicks on the reset button. I am not sure which value to reset to get the timer to completely restart. Thanks in advance. `

private float startTime = 0;

private float restSeconds = 0;

private int roundedRestSeconds = 0;

private float displaySeconds = 0;

private float displayMinutes = 0;

public int CountDownSeconds = 120;

private float Timeleft = 0;
    string timetext = "";

    void Start () {

        startTime = Time.deltaTime;
    }

    void OnGUI () {

       startTime = 0;

       Timeleft = Time.time-startTime;

       restSeconds = CountDownSeconds-(Timeleft);

       roundedRestSeconds = Mathf.CeilToInt(restSeconds);
       displaySeconds = roundedRestSeconds % 60;
       displayMinutes = (roundedRestSeconds / 60)%60;

       timetext = (displayMinutes.ToString() + ":");

       if (displaySeconds > 9){
           timetext = timetext + displaySeconds.ToString();
       }
       else {
           timetext = timetext + "0" + displaySeconds.ToString();
       } 
       GUI.Label(new Rect(10, 10, 200, 40), timetext);

       if(GUI.Button(new Rect(50, 10, 200, 40), "Reset")){
         Debug.Log("Reset Time");
       }
    }`
more ▼

asked Feb 05 '12 at 01:04 AM

masdx gravatar image

masdx
3 2 3 3

(comments are locked)
10|3000 characters needed characters left

3 answers: sort voted first

The time is calculated from the difference of Time.time and startTime. To reset your timer, you need to make this difference be zero, i.e.

void ResetTimer()
{
    startTime = Time.time;
}

void OnGUI()
{
    // your code here
    //...

    if(GUI.Button(new Rect(50, 10, 200, 40), "Reset")){
        Debug.Log("Reset Time");
        ResetTimer();
    }
more ▼

answered Feb 05 '12 at 08:37 PM

Kryptos gravatar image

Kryptos
7.2k 5 32

(comments are locked)
10|3000 characters needed characters left

This will work for you 100%. D

private gameTime=120;

void Start () 
{
    CountDownSeconds=gameTime;

    startTime = Time.time;
}

void Update () 
{

    Timeleft = Time.time-startTime;
    restSeconds = CountDownSeconds-(Timeleft);
    roundedRestSeconds = Mathf.CeilToInt(restSeconds);
    displaySeconds = roundedRestSeconds % 60;
    displayMinutes = (roundedRestSeconds / 60)%60;
    timetext = (displayMinutes.ToString() + ":");
    if (displaySeconds > 9)
    {
       timetext = timetext + displaySeconds.ToString();
    }
    else 
    {
       timetext = timetext + "0" + displaySeconds.ToString();
    } 
    GUI.Label(new Rect(10, 10, 200, 40), timetext);

   if(GUI.Button(new Rect(50, 10, 200, 40), "Reset")){
     Debug.Log("Reset Time");
ResetTimer();
   }
    if(roundedRestSeconds==0)
    {
       Application.LoadLevel("LevelLost");
    }
}
public void ResetTimer()
{
    startTime=Time.time;
    CountDownSeconds = gameTime;
}

Let me know if its working fine.

more ▼

answered Jun 01 '12 at 06:42 AM

hirenkacha gravatar image

hirenkacha
701 12 18 21

(comments are locked)
10|3000 characters needed characters left

Handle the timer vars in an Update function because OnGUI can be called several times per frame. You can set startTime = 0 if the reset button is pushed.

more ▼

answered Feb 05 '12 at 01:14 AM

DaveA gravatar image

DaveA
26.4k 151 171 256

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x4146
x347
x167
x62

asked: Feb 05 '12 at 01:04 AM

Seen: 2823 times

Last Updated: Jun 01 '12 at 06:42 AM