In my game I have watch video earn coins system, now I want to program a delay so the user cant watch the video every 5 seconds, for example I want to let him watch every 6 hours, but I'm not sure how can I do that, how can I program a timer that will count down even if the app is closed or how does this kind of timer work, I mean everyone saw this in many games where they count down days or hours until some gift or something. Any help is appreciated.


2 Answers 2


There is few ways to do this. Store information of last gifting time to:

  1. Local device ( pc, phone, table or what ever is the gaming device)
  2. Cloud service. However, this is harder method and requires the actual service hosted at somewhere in internet.

File writing method is quite simple to do.

  1. Store time to variable
  2. When game exits, write time to textfile
  3. When game starts again, read that file and get the time
  4. Do what you wanna do with the time.

string text = "This is just example text";
// WriteAllText creates a file, writes the specified string to the file, 
// and then closes the file.
System.IO.File.WriteAllText(@"C:\Users\Public\TestFolder\WriteText.txt", text);

// Read the file as one string. 
string text = System.IO.File.ReadAllText(@"C:\Users\Public\TestFolder\WriteText.txt");

Now, if this is very important to keep player doing this over and over again, you cannot store that time as a plain text. You have to use something to scramble it.

  • \$\begingroup\$ I though about your suggestion, but I would also have to get current day month and year in order to compare the time, I mean if I store time at lets says 14:30 , and I only store time and I want to delay 6 hours next time when the user can watch the video is 20:30, but what if my user does not open the game until next day lets say at 15:15 he would still need to wait until 20:30 if I only compare the time, thats why I need also date/month and year, correct me if I'm wrong. \$\endgroup\$ Commented Apr 21, 2015 at 5:17
  • 1
    \$\begingroup\$ Well, with "time" i did not mean clock, but time of event or time of next event. So just safe full datetime, with all the information you need. \$\endgroup\$
    – Katu
    Commented Apr 21, 2015 at 5:25
  • \$\begingroup\$ Thank you for your suggestion, I have experimented a little and I can use TimeSpan, so each time user watches I will save time span and then when he wants to post next time I will just subtract from the TimeSpan in that moment and check the difference. \$\endgroup\$ Commented Apr 21, 2015 at 5:56

Below code will do what you want.

You will need to save and load the value of "DateTime lastPlayDate" between sessions.

using System;

public class GameManager : MonoBehaviour {

    DateTime lastPlayDate;

    void Start()
        lastPlayDate = DateTime.Now;

    void Update()
        DateTime currentTime = DateTime.Now;
        TimeSpan timePassed = currentTime - Convert.ToDateTime(lastPlayDate);
        if (timePassed.TotalDays > 0 timePassed.TotalHours > 0 && timePassed.TotalMinutes > 1)
            Debug.Log("Well that went quick...");

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .