Search Unity

Creating a basic start/top timer

Discussion in 'Scripting' started by unity_4r_hs_ak-C7uBg, Jan 24, 2018.

  1. unity_4r_hs_ak-C7uBg

    unity_4r_hs_ak-C7uBg

    Joined:
    Jan 24, 2018
    Posts:
    23
    Hey I'm relatively new to scripting, especially the time and math part. Basically I'm trying to add the stopwatch functionality. Where the play hits a button, and the timer starts counting up from 0 seconds. There's another button that would also pause the timer. And (if it's not too difficult) an additional button to reset the time back to 0 seconds. This going to be used for tracking a sport athletes time on the court, that's why it needs to be able to start and stop at will. I've been looking all over for something similar, but most timers I find start immediately, and aren't actually a part of the build.
    This is what I have currently for starting the timer. What happens is the timer updates when I click the button, but I'm having trouble getting the desired effect of it counting up. And as far as pausing it with another button and resetting it I haven't even gotten close to figuring that out. Any help would be appreciated.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5. using TMPro;
    6.  
    7.  
    8. public class StopwatchStart : MonoBehaviour {
    9.  
    10.     public Button stopWatchStart;
    11.     public TMP_Text timer;
    12.     private float startTime;
    13.     private string minutes;
    14.     private string seconds;
    15.  
    16.  
    17.  
    18.     // Use this for initialization
    19.     void Start () {
    20.        
    21.         stopWatchStart.onClick.AddListener(StartTimer);
    22.     }
    23.    
    24.     // Update is called once per frame
    25.     void Update () {
    26.        
    27.         float t = Time.time - startTime;
    28.         minutes = ((int)t / 60).ToString();
    29.         seconds = (t % 60).ToString("f0");
    30.         timer.text = minutes + ":" + seconds;
    31.      
    32.     }
    33.     void StartTimer()
    34.     {
    35.         startTime = Time.time;
    36.        
    37.  
    38.     }
    39. }
     
  2. Fido789

    Fido789

    Joined:
    Feb 26, 2013
    Posts:
    343
  3. unity_4r_hs_ak-C7uBg

    unity_4r_hs_ak-C7uBg

    Joined:
    Jan 24, 2018
    Posts:
    23
    I just went and took a look. That seems be be using System.Diagnostics, which, if I'm not mistaken, is usable in the editor, but not when the game is built. So unfortunately I don't believe that will help. Thank you for your reply, however.
     
  4. Fido789

    Fido789

    Joined:
    Feb 26, 2013
    Posts:
    343
    It is just a namespace, it has nothing to do with usability of this class.
     
  5. unity_4r_hs_ak-C7uBg

    unity_4r_hs_ak-C7uBg

    Joined:
    Jan 24, 2018
    Posts:
    23
    Alright, so if someone stumbles upon this while trying to make a stopwatch after working all day and not being able to think properly, I did finally make one that works exactly the way I wanted, and it was a lot easier that I was making it out to be. Keep in mind in my code I'm using TextMesh Pro for the text boxes, but you could easily change that out for a standard text box. (Although TextMesh Pro is amazing.)
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. using TMPro;
    4. //The Goal is going to be to redo this by counting up with time.deltatime. Then doing if else statements for converting 60 seconds into
    5. //one minute and sending those two to the respective number slots. Good luck, buddy
    6.  
    7. public class StopwatchStart : MonoBehaviour {
    8.  
    9.     public Button stopWatchStart;
    10.     public Button stopWatchReset;
    11.     public Button stopWatchStop;
    12.     public TMP_Text timer;
    13.     private float startTime;
    14.     private float minutes;
    15.     private float seconds;
    16.     private bool watchGoing = false;
    17.     private string secondsString;
    18.     private string minutesString;
    19.  
    20.     // Use this for initialization
    21.     void Start () {
    22.         //startTime = Time.timeSinceLevelLoad;
    23.         stopWatchStart.onClick.AddListener(StartTimer);
    24.         stopWatchReset.onClick.AddListener(ResetTimer);
    25.         stopWatchStop.onClick.AddListener(StopTimer);
    26.         minutes = 00;
    27.         seconds = 00;
    28.     }
    29.    
    30.     // Update is called once per frame
    31.     void Update () {
    32.         timer.text = minutesString + ":" + secondsString;
    33.         if (watchGoing == true)
    34.         {
    35.             seconds += Time.deltaTime;
    36.         }
    37.         if ((int)seconds == 59)
    38.         {
    39.             seconds = 0;
    40.             minutes++;
    41.         }
    42.         secondsString = seconds.ToString("00");
    43.         minutesString = minutes.ToString("00");
    44.     }
    45.     void StartTimer()
    46.     {
    47.         watchGoing = true;
    48.         stopWatchStart.gameObject.SetActive(false);
    49.         stopWatchStop.gameObject.SetActive(true);
    50.     }
    51.     void ResetTimer()
    52.     {
    53.         watchGoing = false;
    54.         minutes = 00;
    55.         seconds = 00;
    56.         if (stopWatchStop.enabled == true)
    57.         {
    58.             stopWatchStop.gameObject.SetActive(false);
    59.             stopWatchStart.gameObject.SetActive(true);
    60.         }
    61.         else
    62.         {
    63.             stopWatchStart.gameObject.SetActive(false);
    64.             stopWatchStop.gameObject.SetActive(true);
    65.         }
    66.     }
    67.     void StopTimer()
    68.     {
    69.         watchGoing = false;
    70.         stopWatchStop.gameObject.SetActive(false);
    71.         stopWatchStart.gameObject.SetActive(true);
    72.     }
    73. }
    74.