Make Script Component Do Things Even When Game Is Closed

I want to add a lives system like candy crush in my game. I want to use an InvokeRepeating() function to add lives to an int called lives every second, but when the game is off it can’t add lives because the game object is destroyed.

Is there any way to make a script or game object active even when the game is off? If there is no way to do this, I might put something like this in feedback.

I have not made my script yet but this is what it my script might look like:

using UnityEngine;
using System.Collections;

public class LivesManager : MonoBehaviour {
    
    void addLife () {
        PlayerPrefs.SetInt("Lives", Playerprefs.GetInt("Lives") + 1);
    }
    
    void Start () {
        PlayerPrefs.SetInt("Lives", 0);
        InvokeRepeating("addLife", 1, 1);
    }
}

Do you realize what you are asking ? It’s like asking your car to continue to go while it is “turned off”.

  1. Save the date when the game is “paused” or quitted using OnApplicationFocus
  2. Compute the duration the game has been turned off
  3. Compute the lives to add to your counter depending on the previous duration

Try using a method called Awake(). Might not be exactly what you are looking for, but hopefully it will work.