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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Creating a simple timer

Discussion in 'Scripting' started by MIST0, May 26, 2015.

  1. MIST0

    MIST0

    Joined:
    Apr 26, 2014
    Posts:
    53
    I am creating a simple 2d driving game. I just want to display a timer when the scene begins and the finish line to be the trigger to stop it.

    Am new to unity and coding, just need the simplest way to do it. Thanks
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,744
    There are two options: you can continuously add time each frame using Time.deltaTime, or you can record the start and end times based on Time.time and subtract them.

    Using Time.deltaTime is conceptually simpler. Basically, when you hit start, you'll set your own time tracking variable (say, currentRaceTime) to 0. Then, in Update() as long as your car is in the race, you add Tiem.deltaTime to this variable. At the end of your race, this variable will contain your race time.

    In C#, it'd look something like...
    Code (csharp):
    1. float currentRaceTime = 0f;
    2. void Start() {
    3. currentRaceTime = 0f;
    4. }
    5. void Update() {
    6. if (raceIsGoingOn) {
    7. currentRaceTime += Time.deltaTime;
    8. }
    9. if (raceHasFinished) {
    10. yourUITextObject.text = currentRaceTime.ToString("#.00");
    11. }
    12. }
     
    partidosolo likes this.
  3. sluice

    sluice

    Joined:
    Jan 31, 2014
    Posts:
    416
    I think he wants a timer that runs from the start of the race to the end?..

    1. Create a new C# script and call it SimpleTimer.
    2. Paste this in it:
    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.UI;
    4.  
    5. public class SimpleTimer : MonoBehaviour
    6. {
    7.     public bool isRacing;
    8.     Text txt;
    9.  
    10.     void Awake()
    11.     {
    12.         txt = GetComponent<Text>();
    13.     }
    14.  
    15.     void Start()
    16.     {
    17.         isRacing = true;
    18.     }
    19.  
    20.     void Update ()
    21.     {
    22.         if(isRacing)
    23.             txt.text = Time.time.ToString("#.00");
    24.     }
    25. }
    26.  
    3. Right click on a empty spot of your Hierarchy, Select UI/Text
    4. Put the above script on a that new GameObject that has Text component on it.
    5. Test. You can simulate the race end, by checking isRacing in your inspector panel.


    @StarManta,
    Thanks for the
    Code (csharp):
    1. ToString("#.00")
    tip!
    Much more simple than rounding up!
     
  4. MIST0

    MIST0

    Joined:
    Apr 26, 2014
    Posts:
    53
    Hi I can't add the script to the Gameobject with a text component because of compile errors

    The type or namespace name `Text' could not be found. Are you missing a using directive or an assembly reference?
     
  5. sluice

    sluice

    Joined:
    Jan 31, 2014
    Posts:
    416
    -Have you copied my script exactly as is?

    -Do you have this line at the beginner of your code?
    Code (csharp):
    1. using UnityEngine.UI;
    -Is the GameObject with the text in a Unity Canvas?

    -Does the GameObject have a UI/Text component on it?

    -Are you using Unity 4.6.x or later?
     
    MIST0 likes this.
  6. MIST0

    MIST0

    Joined:
    Apr 26, 2014
    Posts:
    53
    Yeah I wasnt displaying using UnityEngine.UI;

    Am using unity v5

    Yeah no errors now, but nothing is displayed, am looking for a stopwatch effect. UIText component is a childobject of the Camera
     
  7. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,744
    It shouldn't be. Read up on using the UI. Basically, the referenced Text component should be a child of a Canvas object.
     
  8. MIST0

    MIST0

    Joined:
    Apr 26, 2014
    Posts:
    53
    Ok thanks, I have the timer displaying and counting up.

    Now I need to have the timer stop when the car hits a trigger/object. I could do with some pointers on how to do this thanks.