# Check How Many Minutes Have Passed

I have the following script below set the currentDate to the current time when the user launches the app and sets the time they had when clocking it to the oldTime. Currently I’m trying to figure out how many minutes have passed Ex: 1:00PM - 2:00PM is a total of 60 minutes. I’m trying to figure out how the game Candy Crush worked with it’s life timer where you get 1+ life after every half hour, basically I’m trying to produce the same effect except with every 5 minutes. Looked around for a good amount of time and I didn’t find much that would help me.

``````using UnityEngine;
using System.Collections;
using System;

public class LifeTimer : MonoBehaviour {

DateTime currentDate;
DateTime oldDate;

double minutes;
void Start ()
{
currentDate = System.DateTime.Now;
long temp = Convert.ToInt64(PlayerPrefs.GetString("sysString"));
DateTime oldDate = DateTime.FromBinary(temp);
print("oldDate: " + oldDate);
TimeSpan difference = currentDate.Subtract(oldDate);
print("Difference: " + difference);

}

void OnApplicationQuit()
{
PlayerPrefs.SetString("sysString", System.DateTime.Now.ToBinary().ToString());
print("Saving this date to prefs: " + System.DateTime.Now);
}

void Update()
{

}

void OnGUI()
{
long temp = Convert.ToInt64(PlayerPrefs.GetString("sysString"));
GUI.Label(new Rect(10, 10, 300, 30), "Last Time Logged: " + DateTime.FromBinary(temp));
GUI.Label(new Rect(10, 40, 300, 30), "Current Time: " + System.DateTime.Now);
}
}
``````

I’ve coded the below example which will calculate the difference in minutes from the time the application is started to the current time, then every frame it checks if a life should be given against the time and how many lives have already been given. I suspect in your case you’d want to set oldDate, lives, and livesGiven to a PlayerPref containing these values from when they first started playing.

Note that player prefs are not secure and a skilled player could go in and edit his time, lives, or livesGiven.

``````using UnityEngine;
using System.Collections;
using System;

public class LifeTimer : MonoBehaviour {

DateTime oldDate;
DateTime currentDate;

int minutes;

int livesGiven;
int lives;

void Start ()
{
lives = 0;
minutes = 0;
oldDate = System.DateTime.Now;

}

void Update()
{
currentDate = System.DateTime.Now;
minutes = currentDate.Minute - oldDate.Minute;

if(minutes/5 > livesGiven) {
lives++;
livesGiven++;
}
}

}
``````