Cant get Time.time to set to 0 once new round starts..

New to unity. Please help…


I want to make it so when i press space it resets Time.time to 0.

You can’t change Time.time, since it is read-only: Unity - Scripting API: Time.time

However you can use Time.timeSinceLevelLoad instead: Unity - Scripting API: Time.timeSinceLevelLoad

That will give you the time since you last loaded a scene, as you are doing when the user hits the spacebar. By the way, just some extra advice: Application.LoadLevel is obsolete. You should use SceneManager.LoadScene instead.

If you want more fine-grained control, you can simply create your own variable (a float), save the current Time.time value to it when an event you care about happens, then compare Time.time to the time you saved to figure out how much time has passed since that event.

2 Likes

Everything that @PraetorBlue said above is spot-on, plus for that custom time variable, you can just increment it yourself too:

float time;   // class variable
void Update()
{
  time += Time.deltaTime;
}
2 Likes

Well i got it working like this:


but now it shows something like 2.023913.
Thank you for helping me, i will try with the time since level load thing.
Also why is Application.LevelLoad worse than Scene.LoadScene?
Edit:
Used it but it behaves same way as with in the photo.
First time it shows 2.98 then 1.48239. How can i convert a float to int?

Round down to int:

I’m sure there’s also an overload to float.ToString() that can discard decimals, don’t know it by heart.

Also, please look at the sticky post about code tags rather than posting your code as screendumps.