- Home /
How do I stop 'timeSinceLevelLoad' without pausing entire game?
I have been using timeSinceLevelLoad to make a stopwatch score counter. When the game ends I want it to freeze the counter so that I can show the player their final score. When I use Time.timeScale = 0F; it pauses the timer but I need the rest of the game to continue instead of freezing entirely. How would I go about pausing the timeSinceLevelLoad without freezing everything else?
Thanks in advance!
Answer by NoseKills · Aug 30, 2016 at 04:21 PM
I don't think you can. You have to use deltaTime and accumulate it only when the game is not paused or do something like this.
float playedSec;
float pausedSec;
bool isPaused;
void SetPaused(bool pause) {
if (pause && !isPaused) {
playedSec += Time.timeSinceLevelLoad - pausedSec - playedSec;
} else if (!pause && isPaused) {
pausedSec += Time.timeSinceLevelLoad - pausedSec - playedSec;
}
isPaused = pause;
)
Thanks @Nose$$anonymous$$ills for the help. You were right it can't be done. Found a simple solution which was to simply deactivate that script which leaves the time there as if paused.
Answer by Cynikal · Aug 30, 2016 at 05:45 PM
Time.realtimeSinceStartup is not affected by time scale. This however does start from when your program was first loaded.
So...
What you could do is: Record the Time.realtimeSinceStartup at the start of each level load. Then split the difference to determine how much time was spent on the specific level.
void Start()
{
LevelLoadedTime = Time.realtimeSinceStartup;
}
void Update()
{
TimeSpentOnLevel = LevelLoadedTime - Time.realtimeSinceStartup;
}
Your answer
Follow this Question
Related Questions
How to create an accurate timer in Unity? 1 Answer
Need help affecting 2d jump with UnscaledDeltatime 0 Answers
Accurate timing while using timeScale. 1 Answer
timer not ticking down 2 Answers
Unable to change Time.timeScale 2 Answers