# I need my timer to go up every 30 seconds

I wrote code for a jackpot to increase from one date to the next, and it works beautifully. The client wants the numbers to stay put for a while, and then increase.

This is the code:

if (timerRunning1)
{
timerValue1 += Time.deltaTime * 14.4 * (timerValue1F - timerValue1)
/ (finish1 - start).TotalSeconds;

Jack1.text = timerValue1.ToString ("F2");

//PlayerPrefs.SetFloat ("Jackpot1", float.Parse(Jackpot1.text));
//PlayerPrefs.SetFloat ("Jackpot1f", float.Parse(Jackpot1F.text));

if (timerValue1 > (timerValue1F - 0.1))
{
timerRunning1 = false;
print ("ALTO");

PagPrincipal.SetActive (false);
Victoria.SetActive (true);

Debug.Log ("FUNCIONA!!!");

Invoke ("apagavictoria", 10f);

}
}


I tried WaitForSeconds(30f), and it didn't work. How can I make my timer go up every 30 seconds?

• I recommend using a Coroutine for WaitForSeconds to work. – Genevra Nov 26 '16 at 23:10
• You say 'the client wants..', who is the client? Is this being created with the intention of being a professional lottery game? – Gnemlock Nov 27 '16 at 0:36

Ultimately, there are several ways to do this. WaitForSeconds can be used inside a Co-routine, but I would recommend simply keeping your own count, or using MonoBehaviour.Invoke().

Each method essentially requires a class that inherits from MonoBehaviour, but you should be doing so, regardless. You will also want a function that increases the jackpot. We will assume this function is called IncreaseJackpot(). All you need to do, now, is know when to call it.

# MonoBehaviour.Invoke and MonoBehaviour.InvokeRepeating

Let's take a look at the Unity API.

## MonoBehaviour.Invoke

public void Invoke(string methodName, float time);
...
Description
Invokes the method methodName in time seconds.
- Unity API Reference: MonoBehaviour.Invoke

## MonoBehaviour.InvokeRepeating

public void InvokeRepeating(string methodName, float time, float repeatRate);
...
Description
Invokes the method methodName in time seconds, then repeatedly every repeatRate seconds.
- Unity API Reference: MonoBehaviour.InvokeRepeating

Using these methods, you could repeatedly call Invoke("Increase Jackpot", 30f), or you could simply call InvokeRepeating("IncreaseJackpot", 30f, 30f).

# Keeping a Manual Timer

It is also fairly easy to keepbtrack of time using Time.deltaTime and your Update() function, as Time.deltaTime tells us the time since the last call to Update().

float jackpotTimer = 0f;

public void Update()
{
jackpotTimer += Time.deltaTime;

if(jackpotTimer >= 30f)
{
IncreaseJackpot();
jackpotTimer -= 30f;
}
}

• I will add a section for the coroutine solution, when I can. – Gnemlock Nov 26 '16 at 23:16
• Invokerepeating worked fine, but now the jackpot doesn't have animation as it is increasing. Is there another line of code that I can add so that during a certain period of seconds it shows increasing the amount it gained in the seconds it was waiting? – Thatmexicanuzer Nov 26 '16 at 23:31
• @user94487, you would have to use the manual timer option and account for it in your logic. – Gnemlock Nov 26 '16 at 23:41
• You could find the "percent completion", for example, as jackpotTimer * (10f/3f). – Gnemlock Nov 26 '16 at 23:43
• Rather than setting jackpotTimer` to 0 if it's >= 30, subtract 30 from it. This will avoid issues where the framerate is lagging and the time jumps from 29 seconds to 31, as well as truncating off fractions of seconds, making it not quite every 30 in the long run. – Draco18s no longer trusts SE Nov 27 '16 at 0:33