- Home /
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);
}
}
Answer by Cains · Nov 10, 2013 at 08:52 PM
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++;
}
}
}
If the hour is changed I dont think this is going to work.
For example; if the app is opened at 10:51am, when Start() is called your oldDate.$$anonymous$$inute will be 51. currentDate.$$anonymous$$ute will be the updated $$anonymous$$ute every frame so 52, 53, 54, 55, 56... so on.
Once it becomes 11:00am, your currentDate.$$anonymous$$ute becomes 0. So now your calculation "$$anonymous$$utes = currentDate.$$anonymous$$inute - oldDate.$$anonymous$$inute;" returns 0 - 51 = -51. Its going to return a negative int which will never be > livesGiven and thus a life wont be given. .