Generating Lives by Subtracting Time from Time

I am trying to keep track of the time when the game is off. I want to regenerate a life every 30 minutes, by the counter seems to sometimes be off by nearly a minute. I think it has something to do with the way I am subtracting time, but I am not sure. Here is my code for when the game loads.

            int seconds = gameStart.Subtract(lostLife).Seconds;
            int minutes = gameStart.Subtract(lostLife).Minutes;
            if (minutes >= 30)
            {
                lives += (int) (minutes / 30);
                if (lives > maxLives)
                {
                    lives = maxLives;
                }
            }
            if (lives < maxLives)
            {
                float x = (float)((minutes % 30) * 60 + seconds);
                print(x);
                StartCoroutine(gainLives(1800-x));
            }

Thanks!

Make sure you put * Time.deltaTime to the place where you are substracting the time. Otherwise it will be affected by your FPS. For example:

float remainingTime = 60;

remainingTime -= Time.deltaTime;

Not fully sure what you mean by when the game is off, I assume you mean the difference between (Play/Load/Pause/Menu), which each of these things should be within your main script for handling, each as a state.

personally, I would have this function be in that main script(which runs the whole time), and just have your counter++ or time checker, to only run while the state you wish it to is active. So if I understand correctly:

if (gameState == GameState.Load)
{
    timer++;
    if (timer > wait)
    {
         if (lives < maxLives) { lives++;  }
         timer = 0;
    }
}

^ roughly put. You will obviously need to play around with the variables to get it working correctly, but it really should be as easy as that :slight_smile: