Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Creating a timer

Discussion in 'Scripting' started by pokobros, Apr 16, 2014.

  1. pokobros

    pokobros

    Joined:
    Jan 30, 2014
    Posts:
    119
    I want to create a timer that runs at all times even when the app is closed. I want a timer that can do this so that after 30 minutes a life is replenished. I also want to display this timer with GUI. How can I do this? I know how to create a timer with some code that I will post here, but I do not know how to run it at all times. Also with this code when I call CreateClock() for some reason the clock does not show. Is there a better way to do this?

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class ClockScript : MonoBehaviour {
    5.  
    6.  
    7.     public bool  clockIsPaused;
    8.     public float startTime; //(in seconds)
    9.     public float timeRemaining; //(in seconds)
    10.     public bool  timeIsUp;
    11.  
    12.     public string timeStr;
    13.  
    14.     //Screen sizes
    15.     public int sixthOfScreenW;
    16.     public int sixthOfScreenH;
    17.  
    18.     public GUIStyle labelStyle;
    19.    
    20.     public bool displayed;
    21.    
    22.     private bool start;
    23.  
    24.     public void  CreateClock (){   
    25.         sixthOfScreenW = Screen.width / 6;
    26.         sixthOfScreenH = Screen.height / 6;
    27.    
    28.         clockIsPaused = false;
    29.         timeIsUp = false;
    30.    
    31.         startTime = Time.time + 61.0f;
    32.        
    33.         displayed = true;
    34.        
    35.         start = true;
    36.     }
    37.    
    38.     void  OnGUI (){
    39.         if (displayed) {
    40.             labelStyle.fontSize = Screen.width / 20;
    41.             GUI.Label ( new Rect(sixthOfScreenW * 4, sixthOfScreenH * 0.5f, sixthOfScreenW, sixthOfScreenH * 0.5f), timeStr, labelStyle);
    42.         }
    43.     }
    44.  
    45.     void  Update (){
    46.         if (start) {
    47.             if (!clockIsPaused)
    48.             {
    49.                 // make sure the timer is not paused
    50.                 DoCountdown();
    51.             }
    52.         }
    53.     }          
    54.        
    55.     void  DoCountdown (){
    56.         timeRemaining = startTime - Time.time;
    57.         if (timeRemaining < 0)
    58.         {
    59.             timeRemaining = 0;
    60.             clockIsPaused = true;
    61.             TimeIsUp();
    62.         }
    63.         ShowTime();
    64.     }
    65.  
    66.     public void  PauseClock (){
    67.        clockIsPaused = true;
    68.     }
    69.     public void  UnpauseClock (){
    70.        clockIsPaused = false;
    71.     }
    72.    
    73.     void  ShowTime (){
    74.         int minutes;
    75.         int seconds;
    76.    
    77.         minutes = (int)timeRemaining/60;
    78.         seconds = (int)timeRemaining%60;
    79.         timeStr = "Time: " + minutes.ToString() + ":";
    80.         timeStr += seconds.ToString("D2");
    81.     }
    82.  
    83.     void  TimeIsUp (){
    84.         timeIsUp = true;
    85.     }
    86. }
     
    Last edited: Apr 16, 2014
  2. Patico

    Patico

    Joined:
    May 21, 2013
    Posts:
    886
    You should not run it all the times. Just save current data and time when app is closing, and restore it when app runs again. After this you will get the difference between them and strarts the timer since this time.
     
  3. pokobros

    pokobros

    Joined:
    Jan 30, 2014
    Posts:
    119
    That would work but I want a timer like in candy crush saga where once the timer is up there is a push notification that says "Your lives were refreshed!"
     
  4. Fluzing

    Fluzing

    Joined:
    Apr 5, 2013
    Posts:
    815
    That info is probably stored on the Candy Crush servers and not on the client.
     
  5. DanielQuick

    DanielQuick

    Joined:
    Dec 31, 2010
    Posts:
    3,137
    The way those notifications work is they are registered while the game is playing.

    Example (not correct code)
    Code (csharp):
    1.  
    2. void OnApplicationPause (bool pauseStatus) {
    3.     if (pauseStatus == true) {
    4.         NotificationManager.RegisterNotification ("This is a Notification", GetTimeUntilLivesRefreshed ());
    5.     }
    6. }
    7.  
     
  6. pokobros

    pokobros

    Joined:
    Jan 30, 2014
    Posts:
    119
    I could just use Patico's way but does anyone know how to it like how I stated above with Candy Crush?
     
  7. Zaladur

    Zaladur

    Joined:
    Oct 20, 2012
    Posts:
    392
    Seeing as how Candy crush can be played without any connection to the servers, I don't see how this could be the case. Pretty sure it uses your system time to determine things like this. Save the start time, add thirty minutes, and compare current time to the new value to get your timer. This will work even when you exit the program and resume some time later on.

    I don't work in mobile, so I'm not sure how push notifications are handled.
     
  8. pokobros

    pokobros

    Joined:
    Jan 30, 2014
    Posts:
    119
    Thanks Zaladur!
     
  9. pokobros

    pokobros

    Joined:
    Jan 30, 2014
    Posts:
    119
    So would I use System.DateTime.Now and set the value to a variable. Then the next time the user opens the app retrieve the new System.DateTime.Now value? If so how would I see if the new value is 30 minutes after the old value?