Had a scenario where you need to log the game time across a game session and display it? Look no further, the below example has you covered.
You will need to add TMPro to use TextMeshPro; alternatively you can use Unity's Text UI element too. The float variable gameTime is used to store our game time in seconds, and the UI_GameTime variable stores our reference to our UI object, which we will use to display the time.
Then, we set gameTime with Time.time in the Update() method. This code can technically be in any MonoBehaviour script with an Update method, but we recommend placing it in a script that handles game UI. Time.time returns the current game time since the start of the application and stores it every frame, we just need to convert that float into a time format. We can use Timespan for this, then set our UI element with the newly formatted string.