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. Dismiss Notice

Time Counter help!

Discussion in 'Scripting' started by Cynicaldev, Feb 15, 2016.

  1. Cynicaldev

    Cynicaldev

    Joined:
    Feb 4, 2016
    Posts:
    7
    Hello

    I've been trying to make a Time Counter, it's working so far so good. But there is one thing i wanna add.
    Whenever the miliseconds reach 100+ miliseconds it will go back to 0 and then start to count again. Same with seconds. Anybody that could help me out cause i've been working alot on a project and my brain is all done.. Lol



    The things i wanna add is similar to a video camera Time Counter.




    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.UI;
    4.  
    5. public class Timer : MonoBehaviour {
    6.  
    7.     public Text counterText;
    8.  
    9.     public float miliseconds ,seconds, minutes;
    10.  
    11.     // Use this for initialization
    12.     void Start () {
    13.         counterText = GetComponent<Text> () as Text;
    14.    
    15.     }
    16.    
    17.     // Update is called once per frame
    18.     void Update () {
    19.         minutes = (int)(Time.timeSinceLevelLoad/60f);
    20.         seconds = (int)(Time.timeSinceLevelLoad % 60f);
    21.         miliseconds = (int)(Time.timeSinceLevelLoad * 100f);
    22.         counterText.text = minutes.ToString("00") + ":" + seconds.ToString("00") + ":" + miliseconds.ToString("00");
    23.  
    24.    
    25.     }
    26. }
    27.  
     
  2. steego

    steego

    Joined:
    Jul 15, 2010
    Posts:
    967
  3. Cynicaldev

    Cynicaldev

    Joined:
    Feb 4, 2016
    Posts:
    7
    Thank you! But im not quite sure what to do with it :( could you help me a little with the code?
     
  4. steego

    steego

    Joined:
    Jul 15, 2010
    Posts:
    967
    Something like this:

    Code (csharp):
    1.  
    2.     void OnGUI()
    3.     {
    4.         float t = Time.timeSinceLevelLoad;
    5.         int ms = Mathf.RoundToInt(t * 1000f) % 1000;
    6.         int s = Mathf.FloorToInt(t) % 60;
    7.         int m = Mathf.FloorToInt(t / 60) % 60;
    8.         string ts = string.Format("{0:00}:{1:00}.{2:000}", m, s, ms);
    9.         GUI.Label(new Rect(0f, 0f, 200f, 20f), ts);
    10.     }
    11.  
     
  5. Cynicaldev

    Cynicaldev

    Joined:
    Feb 4, 2016
    Posts:
    7
    Thank you! I'll try it out :)