I want to make a timer that resets a merchant's item price and quantity.
Like many android games, there are timers that continue to go down when not playing the game. When you are not playing the game, the timer should continue to countdown. How to keep the count down timer going when the game is closed?
Below is my countdown timer
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class margaretSellTimer : MonoBehaviour {
public Text TimerText;
public string dy;
public float days;
public float Hours;
public float Minutes;
public float Seconds;
initMerchantMargaret initMerchants;
player Player;
// Use this for initialization
void Start () {
initMerchants = GameObject.FindGameObjectWithTag ("initMerchant").GetComponent<initMerchantMargaret> ();
Player = GameObject.FindGameObjectWithTag ("player").GetComponent<player> ();
StartCoroutine(Wait());
}
// Update is called once per frame
void Update () {
if (days == 1) {
dy = "day ";
} else if (days > 1) {
dy = "days ";
} else {
dy = "day ";
}
if(Seconds < 10)
{
TimerText.text = (days + " " + dy + Hours + ":" + Minutes + ":0" + Seconds);
}
if(Seconds > 9)
{
TimerText.text = (days + " " + dy + Hours + ":" + Minutes + ":" + Seconds);
}
}
public void CountDown()
{
if(Seconds <= 0) {
if(Minutes > 0) {
MinusMinute();
Seconds = 60;
}
}
if (Minutes <= 0) {
if(Hours > 0) {
MinusHours();
Minutes = 59;
Seconds = 60;
}
}
if (Hours <= 0) {
if(days > 0) {
MinusDays();
Hours = 23;
Minutes = 59;
Seconds = 60;
}
}
if(Minutes >= 0)
{
MinusSeconds();
}
if(Hours <= 0 && Minutes <= 0 && Seconds <= 0 && days <= 0)
{
StopTimer();
}
else
{
Start ();
}
}
public void MinusDays() {
days -= 1;
}
public void MinusHours() {
Hours -= 1;
}
public void MinusMinute() {
Minutes -= 1;
}
public void MinusSeconds() {
Seconds -= 1;
}
public IEnumerator Wait() {
yield return new WaitForSeconds(1);
CountDown();
}
public void StopTimer() {
Seconds = 5;
Minutes = 0;
Hours = 0;
days = 0;
Player.margaretItemSell.Clear ();
Player.productMargaretSell.Clear ();
Player.productSellMargaret.Clear ();
initMerchants.margaretSell ();
Start ();
}
}
Thanks.