Restart timer

Hello,
I am trying to create a timer script.
I have problems to reset the timer during a reset.

Should I create a condition with time = 0; ??

I really don’t know how to write it.

Here is my code :

    float time  ;     
   
    void Awake () {
       
        time = (float)Time.time;
        
                  }
   
   
    void Update(){
if (myCondition) {
       
       
GetComponent<Text> ().text = string.Format("{0:00}:{1:00}:{2:00}",Mathf.Floor(time/60),time%60,(time*100)%100);        
        

    time = (float)Time.time;
  
                 }
   
                 }

Is your reset condition where time <= 0? Then that should be your if condition.

Hi, this is a big problem so you need to decompose it in two small problems. The first one is to reset the timer, and the second is to make that occur during a reset.

For resetting the timer, you figured out yourself you need a method like so:

public void ResetTimer()
{
    time = 0;
}

So, that one is solved, to fix the other small issue you need to know when a reset occurs. Is it when myCondition is false? If so then:

    void Update(){
    if (myCondition) {
      
      
        GetComponent<Text> ().text = string.Format("{0:00}:{1:00}:{2:00}",Mathf.Floor(time/60),time%60,(time*100)%100);            
        time = (float)Time.time;
     }
    else {
        ResetTimer();
    }  

}

If your reset occurs on a button clicked then you need to call ResetTimer() in the button event method.

Brathnann
no, my condition is not on time, but on a state of my games :slight_smile: a bool that I will look for in another script.

dlorre my problem is not the condition method, your code works fine, my problem is that if I do a time = 0; , it does not reset my settings.
I don’t know how to write the code to reset a timer.

The only solution I find would be to integrate into my restart script:

GetComponent<Text> ().text = string.Format("{0:00}:{1:00}:{2:00}",0,0,0);

You can use a setter like so:

        private float time;
        public float Time
        {
            get { return time; }
            set
            {
                time = value;
                GetComponent<Text>().text = string.Format("{0:00}:{1:00}:{2:00}", 0, 0, 0);
            }
        }

that way when you set Time = 0 the display will be updated as well as the time variable.

flottant public Heure ???
there is an error, right ?
I don’t really understand how to integrate this piece of code.

I think you have translated the page? I’ve given you some code here.

Cooldown timers, gun bullet intervals, shot spacing, rate of fire:

GunHeat (gunheat) spawning shooting rate of fire:

dlorre yes i have translated the page.
I studied your code well, but I can’t integrate it.
I spend my float time privately.
but after the variable I don’t really understand:

{...}```
,

"Time" has neither a variable nor a void, and since my programming level is low, I'm having trouble understanding why it's structured like that.
I tried to copy this code without success.

[Kurt-Dekker](https://forum.unity.com/members/kurt-dekker.225647/)
thank you but I'm not sure that the resources you gave me are useful.

__What I'm trying to do is restart this :__

![8192763--1068192--02c7fdad6c8899d8e1dd50845f54e242_w200.gif|200x150](upload://vGZC7SdmiZjggiOyDSaS5M9SyiY.gif)

I tried to find an answer in the doc but without success : Unity - Scripting API: Time

Oh come on, don’t name a local identifier the same as a UnityEngine class (Time)!

That’s just needlessly intentionally confusing.

Look at the problems you caused:

@venom789 just make a float for your timer, and DO NOT CALL IT Time because that is already in use by the system.

public float myTimer;   // properly named timer

When you want it to count down for 7 seconds ,do this:

myTimer = 7.0f;

In Update():

if (myTimer > 0)
{
   myTimer -= Time.deltaTime;
}

To find if it is “expired” or still counting down:

 if (myTimer > 0)
{
  Debug.Log( "timer is still running...");
}
else
{
  Debug.Log( "timer is expired...");
}

That’s IT. If you want to display it all spiffy sharp like 00:00:00, go look at the C# time formatting documentation… all this stuff can be done by the .NET library without you writing crazy "{0:00}:{1:00}" kinds of nonsense.

Kurt Dekker
thank you but I’m not trying to make a count down , I’m creating a speed runner.
I created a watch, and I want it to reset to 0 when my level restarts.

That’s even EASIER!

public float elapsedTime;]

in Update():

elapsedTime += Time.deltaTime;

As long as this MonoBehaviour is part of your scene and loads, elapsedTime will start at zero.

If you need to zero it… um… zero it!

1 Like

Kurt-Dekker indeed it is simpler like that, it works ^^ thank you

1 Like

@Kurt-Dekker ah good catch, I made this on the fly without thinking it was in use by Unity. The purpose of using a setter is to make sure that the object will update the display when the variable changes.

1 Like

thanks anyway dlorre, I will study the setter and getter a little more ^^