store a playerpref for the next time and check it in you main game manager scripting… a lot of games like that have a “work around” where you can just change the time on the phone/device to get another life after all
you can use System.DateTime.Now.Minute, store the start time when the app or level or what ever loads. then do a test to see if the time is half an hour later or something then do your logic with that.
or just create a custom timer counting up to 30 minutes.
The System.DateTime.Now.Minute is nice to use but can be problematic if the hour changes or day changes etc
I imagine it wouldn’t run if the app was closed no.
You could store what the time was using player prefs in the OnApplicationQuit () function then carry it on when Awake is called? Not sure how nicely that would work out.
Personally I would just use the System.Date class, I’ve used it before, It’s not to bad to use. Here’s an example of how I’d do it:
using UnityEngine;
using System.Collections;
public class TimeCalc : MonoBehaviour {
void Start () {
//Calculate Time difference between 13:20 and 15:30
Debug.Log("Time Difference is : " + CalculateTimeDiff(13, 20, 15, 30) + " Mins");
}
int CalculateTimeDiff (int hour1, int min1, int hour2, int min2) {
int newHour = Mathf.Abs(hour1 - hour2);
int newMin = Mathf.Abs(min1 - min2);
int totalDiff = (newHour * 60) + newMin;
return totalDiff;
}
}
Sorry for the hideous code; I’ll try clean it up later. I’m sure there is MUCH better way to calculate time
using UnityEngine;
using System.Collections;
[System.Serializable]
public class TimeObject {
public int hour;
public int min;
}
public class TimeCalc : MonoBehaviour {
public TimeObject timeStart, timeEnd;
void Start () {
Debug.Log("Time Difference: " + CalculateTimeDiff(timeStart, timeEnd) + " Minutes");
}
int CalculateTimeDiff (TimeObject time1, TimeObject time2) {
return Mathf.Abs(Mathf.Abs(time1.hour - time2.hour)) * 60 + Mathf.Abs(time1.min - time2.min);
}
}
I have attached this(TimeCalc) script to empty object & from player controller script
void Update ()
{
if(lifeCounter==2)
{
timeCalc.enabled=true;
}
}
But I get error like:
NullReferenceException: Object reference not set to an instance of an object
PlayerController.Start () (at Assets/Codes/Scripts/PlayerController.cs:76)
Oh my bad; I forgot to check if you were a beginner.
you’ll probably want to add my code to your own class. paste the CalculateTimeDiff function into your class somewhere.
And add this at the very top of your script, above your class definition like I did above.
[System.Serializable]
public class TimeObject {
public int hour;
public int min;
}
I normally wouldn’t just write code for someone but since I started I might aswel finish. you really need to go learn the basics again; it seems like that might be something you’re struggling with?
Here’s a implementation using my way (Note I have not tested it, I’ve just wrote it in the text editor. There maybe errors)
using UnityEngine;
using System.Collections;
[System.Serializable]
public class TimerObject {
public int Hour;
public int Min;
public TimerObject(int hour, int min) {
this.Hour = hour;
this.Min = min;
}
}
public class PlayerController : MonoBehaviour {
private int timeLockFrom;
private int gameIsLoc_UnLoc;
public TimerObject timeStart, timeEnd;
private bool isLocked = false;
void Start () {
if(isLocked)
timeEnd = new TimerObject(System.DateTime.Now.Hour, System.DateTime.Now.Minute); //Set the starting time to the current time.
//Check to see if we can unlock the timer
if(CalculateTimeDiff(timeStart, timeEnd) > 30)
isLocked = false;
}
void Update () {
if(lifeCounter == 2 && !isLocked) {
timeStart = new TimerObject(System.DateTime.Now.Hour, System.DateTime.Now.Minute); //Set the starting time to the current time.
isLocked = true;
}
}
int CalculateTimeDiff (TimerObject time1, TimerObject time2) {
return Mathf.Abs(Mathf.Abs(time1.Hour - time2.Hour)) * 60 + Mathf.Abs(time1.Min - time2.Min);
}
}