- Home /
Regenerating lives whilst app is closed for iOS
I have an iOS game where the player regenerates 1 life every 10 minutes. This works fine whilst playing the game, but how can I make it regenerate when the app is closed on the iPhone?
This is my script to regenerate?
var delay : float = 0;
var delayedtime : float = 600;
function Start(){
delayedtime = Time.time + delay;
}
function Update(){
if (Time.time > delayedtime){
livesCounter.livesCounter = livesCounter.livesCounter +1;
delayedtime = Time.time + delay;
}
}
The livesCounter.livesCounter is my static var.
Thanks
Answer by whydoidoit · Apr 26, 2013 at 06:13 AM
I'm afraid that this requires you to not use code running in the background. Instead you need to do something like this:
function OnApplicationPause(paused : boolean)
{
if(paused)
PlayerPrefs.SetString("ApplicationPausedTime", System.DateTime.Now.ToString());
else
{
var timeApplicationWasPaused = System.DateTime.Parse(PlayerPrefs.GetString("ApplicationPausedTime", "01/01/2001"));
var timePaused = (System.DateTime.Now - timeApplicationWasPaused).TotalMinutes;
if(totalMinutes < 100000)
{
livesCounter.livesCounter += Mathf.FloorToInt(totalMinutes/10);
}
}
}
You would however also probably want to handle the Quit and restart situations in a similar way as iOS might just kill your app in the background.
Thanks for this! But does this work whilst the game is running, or only when it is closed? Will I need to use both scripts to regenerate lives?
Thanks
Your answer
![](https://koobas.hobune.stream/wayback/20220613101020im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Access the system time on mobile 1 Answer
Specify Keyboard type on textfield for mobile 0 Answers
Detect Device Orientation Event 1 Answer