Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

Trying to get the exactly time

Discussion in 'Scripting' started by ltteixeira, Apr 12, 2018.

  1. ltteixeira

    ltteixeira

    Joined:
    Oct 6, 2017
    Posts:
    4
    Hi, I'm trying to get the exactly time in seconds to be printed in the console window. But as deltaTime method actually gives the time in seconds between each subsequent frame, so... I can't get a precise time count. Any guesses of what I should be doing? P.S: Sorry for my bad english.

    Code (CSharp):
    1. public class PrintTimeScript : MonoBehaviour {
    2.  
    3.     private float getTime = 0f;
    4.     private int index = 0;
    5.  
    6.     // Update is called once per frame
    7.     void Update () {
    8.  
    9.         getTime += Time.deltaTime;
    10.         if (getTime > index){
    11.             print (index += 1);
    12.         }
    13.     }
    14. }
     
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    You're trying to get the time in seconds of what then?

    If you're trying to get the time in seconds since something the player did earlier, you can just add Time.deltaTime to a variable every update from the time you want to start tracking until the time you want to display it.

    If you want the time since the game started, you'd use Time.time.

    If you want the time of day, you'd use DateTime.Now()
     
  3. ltteixeira

    ltteixeira

    Joined:
    Oct 6, 2017
    Posts:
    4
    Actually, I'm just testing. I want to start counting time since the play button is clicked and print only full seconds like:

    1
    2
    3
    4
    (...)

    And not:

    1.46572
    2.56748
    3.73827
    (...)
     
  4. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Ah okay, now that makes some more sense to understand.
    Simply print it as an int, or formatted with no decimal places:
    Code (csharp):
    1. print("time: " + ((int)getTime).ToString());
    2. // or
    3. print("time: " + getTime.ToString("N0"));
    Something like that.