Search Unity

Converting active GUI timer to C#

Discussion in 'Scripting' started by MDragon, Jan 12, 2014.

  1. MDragon

    MDragon

    Joined:
    Dec 26, 2013
    Posts:
    329
    Original: http://answers.unity3d.com/questions/11458/how-to-make-a-timer-in-the-game.html
    (attached to the end of this post for ease)

    I'm trying to convert the answer above to C#. However, I've run into a couple of problems.
    1. What type is guiTime/startTime supposed to be? If it's float, then...
    2. What type should seconds, minutes, fractions be?
    3. And the biggest one of all... How should String.Format be done? Should I just throw that out the window and format it myself?
    4. (more like 3b) If I do convert all the floats to ints to strings and format the text myself, how should I make sure they're rounded properly?
    5. Also I chose to throw in the assignment of startTime in the Start() function since I'm not sure if there's much of a difference but it seems like it'd be better than the Awake if there is any difference time-wise.

    Thanks for all your help!

    The code from the link as-is:
    Code (csharp):
    1.     private var startTime;
    2.     var textTime : String; //added this member variable here so we can access it through other scripts
    3.      
    4.      
    5.     function Awake() {
    6.      
    7.     startTime = Time.time;
    8.      
    9.     }
    10.      
    11.     function OnGUI () {
    12.      
    13.     var guiTime = Time.time - startTime;
    14.      
    15.     var minutes : int = guiTime / 60;
    16.     var seconds : int = guiTime % 60;
    17.     var fraction : int = (guiTime * 100) % 100;
    18.      
    19.     text = String.Format ("{0:00}:{1:00}:{2:000}", minutes, seconds, fraction);
    20.     GUI.Label (Rect (400, 25, 100, 30), textTime); //changed variable name to textTime -->text is not a good variable name since it has other use already
    21.      
    22.     }
     
  2. unitylover

    unitylover

    Joined:
    Jul 6, 2013
    Posts:
    346
    If I had a penny fir everytime I had to play guess the type for javascript variables I'd be a billionaire. Lol! Anyways, guiTime is a float and so is startTime. You could change minutes, seconds and fraction to floats as well and use Mathf.Round() to get properly rounded values. The string format is fine but you could also do it any other way you find comfortable.
     
  3. MeowMixEater

    MeowMixEater

    Joined:
    Dec 23, 2013
    Posts:
    18
    This might help to

    Debug.Log(guiTime.GetType());

    ^ I don't know javascript but I believe that is correct.
     
  4. Zeblote

    Zeblote

    Joined:
    Feb 8, 2013
    Posts:
    1,102
    Just start typing Time.time and it will pop up a small box telling you that it is a float.
     
  5. MDragon

    MDragon

    Joined:
    Dec 26, 2013
    Posts:
    329
    Thanks guys for the suggestions.

    And yes, I actually started to try converting this. I did look up Time.time and made guiTimer and startTimer as floats. However, I ran not only into the problem of converting to int for the min/sec/fraction (which I wanted to use because I didn't know how to round them properly as ints and keep the timer looking correct) and also ran into the issue of String.Format. And then I wasn't sure of anything anymore :)

    So, unitylover, about string format... Did you mean I should simply format it myself - using if-statements and such to see if they >= 60 and then set them back to 0 or such - or that there is a C# equivalent of String.format?
     
  6. Zeblote

    Zeblote

    Joined:
    Feb 8, 2013
    Posts:
    1,102
    Can just do this:

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class blah : MonoBehaviour
    5. {
    6.     private float startTime;   
    7.     private string textTime;   
    8.     void Awake()
    9.     {      
    10.         startTime = Time.time;
    11.     }
    12.     void OnGUI ()
    13.     {
    14.         float guiTime = Time.time - startTime;
    15.         int minutes = (int)guiTime / 60;
    16.         int seconds = (int)guiTime % 60;
    17.         int fraction = (int)(guiTime * 100f) % 100;    
    18.         textTime = System.String.Format ("{0:00}:{1:00}:{2:00}", minutes, seconds, fraction);
    19.         GUI.Label (new Rect (400f, 25f, 100f, 30f), textTime);
    20.     }
    21. }
    22.  
     
  7. MDragon

    MDragon

    Joined:
    Dec 26, 2013
    Posts:
    329
    Ah...
    :eek:

    Thanks a ton! I guess I better check up to see what's available under System. And this actually simplifies everything.

    Once again, thanks a ton.