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?

  • \$\begingroup\$ I recommend using a Coroutine for WaitForSeconds to work. \$\endgroup\$ – user84154 Nov 26 '16 at 23:10
  • \$\begingroup\$ You say 'the client wants..', who is the client? Is this being created with the intention of being a professional lottery game? \$\endgroup\$ – 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.


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


public void InvokeRepeating(string methodName, float time, float repeatRate);
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)
        jackpotTimer -= 30f;
| improve this answer | |
  • \$\begingroup\$ I will add a section for the coroutine solution, when I can. \$\endgroup\$ – Gnemlock Nov 26 '16 at 23:16
  • \$\begingroup\$ 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? \$\endgroup\$ – Thatmexicanuzer Nov 26 '16 at 23:31
  • \$\begingroup\$ @user94487, you would have to use the manual timer option and account for it in your logic. \$\endgroup\$ – Gnemlock Nov 26 '16 at 23:41
  • \$\begingroup\$ You could find the "percent completion", for example, as jackpotTimer * (10f/3f). \$\endgroup\$ – Gnemlock Nov 26 '16 at 23:43
  • 1
    \$\begingroup\$ 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. \$\endgroup\$ – Draco18s no longer trusts SE Nov 27 '16 at 0:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.