# How can I check how long the player has been playing for?

How can I check how long the player has been playing for in Unity?

• What have you tried?
– user35344
Nov 4, 2021 at 16:03
• This seems like a straightforward use of the Time class, no? What do you need that's not adequately explained in the API documentation? Nov 4, 2021 at 16:09
• @Tyyppi_77 the only thing i tried was this : if(Time.time == 0.30f) { Debug.Log("That was 30 seconds"); }. I also tried to find a tutorial on this on youtube Nov 4, 2021 at 16:11
• @DMGregory I dont know what to use to answer my question Nov 4, 2021 at 16:13
• That code checks whether this code is running in a frame that began EXACTLY 300 milliseconds after the game started up. That means it's false if the frame began 300.0001 milliseconds after start up. Did you mean to use if (Time.time >= 30.0f)? Generally for floating point numbers, you want to compare ranges, not exact equality, since being off by 0.00001 is more than enough for exact equality to fail Nov 4, 2021 at 16:21

To know how long the game has been running in wall time as of the start of the current frame, you can use Time.unscaledTime. As described in the documentation, this gives:

The timeScale-independent time for this frame (Read Only). This is the time in seconds since the start of the game.

Because this is unaffected by timeScale, it won't distort your real-world time measurement if you have slow-mo effects in your game, or if performance problems cause your game simulation to lag. It will also count time spent while the game is paused with timeScale = 0, if that's something used in your game.

To check whether 30 seconds have passed, you can write:

bool _thirtySecondsPassed = false;

void Update() {
if (_thirtySecondsPassed == false && Time.unscaledTime >= 30f) {
Debug.Log("Thirty seconds have passed.");
_thirtySecondsPassed = true;
}
}


Here the boolean member variable ensures you print the message just once, not every future frame after the initial 30 seconds are up.

• how about this: how can i make the number 700,000 divided by the seconds the player has been playing for and then add that to the score (it shouldn't have a decimal point). Does that make sense?? this is what I have: if(Time.time > 30f) { userInput.instance.score += (int)(700000 / Time.time); }. But that doesn't take the seconds and divide the number. Nov 5, 2021 at 13:24
• You probably don't want += here. Otherwise you'll get 700k/30 = 23 333 on the first frame after the 30 second mark, then add 700k/30.033 = 23 307 on the very next frame for a total of 46 640, etc, stacking up faster on machines that run at a higher frame rate. You might want = instead. Questions about how to do arithmetic in C# are general programming - there's nothing about division and addition that's unique to game development. You can search our general programming sister site StackOverflow for help with that. Nov 5, 2021 at 13:39
• no because I'm running the if statement once the game is finished and theres bonus points added to the score. And the bonus points are calculated by dividing 700000 by the number of seconds played. This is what I want to do. And my question is : how can I divide 700000 by the number of seconds played. Nov 5, 2021 at 14:33
• value = numerator / denominator - there's no secret trick here.. The example above uses the time since the game executable started up. If you want to measure the time in the current level/run, be sure to subtract a time stamp you saved at the start of the level / run. Nov 5, 2021 at 14:49
• ok. I feel like it would be easier if you would just give me the code. I got no idea what value = numerator / denominator means. And I got no idea what secret trick you think i think there is. Sorry mate, but some people are really dumb for example me. I could easily research it myself if only i had the code Nov 5, 2021 at 15:14