Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Get the total time that a level is played

Discussion in 'Scripting' started by herbie, Oct 28, 2013.

  1. herbie

    herbie

    Joined:
    Feb 11, 2012
    Posts:
    237
    Hi everybody,

    My game consists of several levels.
    When a level starts, also the leveltime starts with Time.timeSinceLevelLoad.
    When you loose the level, the level will restart and you have to play the level again until you win.
    When the level restarts, also the leveltime restarts from zero of course.

    What I want to have is the total time that the level is played until it's won. So when the level restarts, the time should not begin at zero but at the time when the level is lost before.

    I can not use Time.time because every level in the game has it's own time (Time.time is the time since the start of the whole game).

    I can do something with the PlayerPrefs but than I have to know if it is possible to start Time.timeSinceLevelLoad from a certain time and not from zero.

    Does anybody has an idea how to do this?
     
  2. Errorsatz

    Errorsatz

    Joined:
    Aug 8, 2012
    Posts:
    555
    Time.time will work, you just need to save what time the player entered the level. Then subtract it from the current Time.time. If you're supporting saving, then you'd want to save the total time elapsed as part of the data.

    If you want something other than raw time - such as not running the clock while the game is paused - then the best way might be to have a function you invoke once per second that increments the level time if appropriate.
     
  3. herbie

    herbie

    Joined:
    Feb 11, 2012
    Posts:
    237
    In my case I prefer the function that increments the level time every second.
    Thanks for the tip!