public float hour, min, sec; void Update(){ TimerStart(); } void TimerStart() { if (hour >= 0) { sec -= Time.deltaTime; if (sec < 0) { sec = 59; min--; } if (min < 0) { min = 59; hour--; } } } 

Hours, minutes and seconds ask from the inspector. I don't need an ultra-precise timer, but is the principle correct? Or maybe there are some easier ways, built-in functions, etc.?

  • Google in the direction of TimeSpan - aa_talanin
  • Personally, it is interesting to me to begin with why this timer is needed. Depending on the answer, I can answer about which way is better to go) - Andrew
  • @Andrew when passed for example the current bonus level, you can re-play only after, well, 48 hours. The timer is triggered, when leaving the game, it stores hours, minutes, seconds in PlayerPrefs, when entering the game it reads the absence time from the timer. There will still be events, a separate timer will be on the server, there will be an icon that shows the start time of the next tournament and the timer ticks there - maq
  • The man correctly wrote about TimeSpan, use it better. - JediMan4ik 7:13
  • @ JediMan4ik yes, I used that one for the second timer - maq

4 answers 4

@Andrew when passed for example: the current bonus level, you can re-play only after, well, 48 hours. The timer is triggered, when leaving the game, it stores hours, minutes, seconds in PlayerPrefs, when entering the game it reads the absence time from the timer.

My suspicions came true :)

In this particular case, the timer is NOT ALLOWED. All you need is to remember the time when you can do the next iteration. In this case, let there be a call to the BonusGame() method BonusGame()

will be something like this:

 public void BonusGame() { if( DateTime.Now > PlayerPrefsNextBonusGameTime) { //some bonus Game Logic } } 

and to show the time remaining to a possible game in the bonus game, you need something like:

 public TimeSpan TimeLeftToBonusGame { get { return DateTime.Now - PlayerPrefsNextBonusGameTime ; } } 

where PlayerPrefsNextBonusGameTime is DateTime.

everything was written exclusively within the framework of pseudocode and may not work when copying, but here the main thing is to understand the approach.

  • Everything, I understood you) - maq
  • txt.text = string.Format ("{0}", TimeLeftToBonusGame); How to format, so that the seconds show int, that is, 2 whole digits and that there is no minus in front? - maq
  • And yet, people can trick the timer, change the time on the phone (pc)? - maq
  • so it will be adequate? Just multiplied everything by -1: txt.text = string.Format ("{0:00}: {1:00}: {2:00}", TimeLeftToBonusGame.Hours * (- 1), TimeLeftToBonusGame.Minutes * (- 1), TimeLeftToBonusGame.Seconds * (- 1)); - maq
  • no need to format it in any way. Compare the time as a datetime. And read about formatting time. For example, here: stackoverflow.com/questions/923406/… - Andrew

I would make a private field private float secondsLeft , in the Start method I initialize it as secondsLeft = hour*60*60 + min*60 + sec and in the Update method I would add lines

 secondsLeft -= Time.deltaTime if (secondsLeft <= 0) { //do smth } 

Fully:

 public float hour, min, sec; private float secondsLeft; void Start() { secondsLeft = hour*60*60 + min*60 + sec; } void Update() { secondsLeft -= Time.deltaTime if (secondsLeft <= 0) { //do smth } } 

Or use korutiny (I advise you to read about it separately, a very useful thing). I will give just an example:

 public float hour, min, sec; void Start() { StartCoroutine(Timer(hour*60*60 + min*60 + sec)); } private IEnumerator Timer(float secondsToWait) { yield return new WaitForSeconds(secondsToWait); //do smth after timer yield return null } 
  • Yes, I like the first option, but I clearly need to show how many hours, minutes, seconds are left. So probably it will only show in seconds - maq

you just remember the absolute time when you can already enter the bonus game
continue in a separate MyBonusTimeHandler:MonoBehaviour use Invoke('Π½Π°Π·Π²Π°Π½ΠΈΠ΅ ΠΌΠ΅Ρ‚ΠΎΠ΄Π° ΠΊΠΎΡ‚ΠΎΡ€Ρ‹ΠΉ Π°ΠΊΡ‚ΠΈΠ²ΠΈΡ€ΡƒΠ΅Ρ‚ Π²ΠΎΠ·ΠΌΠΎΠΆΠ½ΠΎΡΡ‚ΡŒ',DateTime.Now - PlayerPrefsNextBonusGameTime); in the Start() method
Dvizhek himself will call that method after a specified time in the float if I made a mistake above, I do not remember about overloading

Why pervert and write your own timer when there is a TimeSpan in C #?

Simply call TimeSpan.Add to the already existing time and add the required time.

Do you want to display on the screen? There are many formats in ToString() .