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

Create CountDown Timer (3d Text) in C#

Discussion in 'Scripting' started by Neuronaut12, Oct 27, 2015.

  1. Neuronaut12

    Neuronaut12

    Joined:
    Aug 4, 2015
    Posts:
    23
    I was hoping someone could lend their advice, I'm relatively new to coding and would appreciate the input. I was combing the forum and found a countdown script that works quite well when applied to a 3d text. I was just wondering how to expand it in the following ways:

    1) At 0 seconds, it should stop the timer and display a message
    2) At fixed intervals (arbitrary) it can prompt a message

    The script is the following:

    using UnityEngine;
    using System.Collections;

    public class Timer : MonoBehaviour {
    public float time = 20.0f; // Arbitrary number
    TextMesh tm;

    void Start () {
    tm = GetComponent<TextMesh>();
    }


    void Update () {
    time -= Time.deltaTime;
    tm.text = Mathf.RoundToInt(time).ToString();
    }
    }

    Thank you in advance!
     
  2. Elmdran

    Elmdran

    Joined:
    Oct 28, 2014
    Posts:
    34
    Code (CSharp):
    1. void Update () {
    2.     time -= Time.deltaTime;
    3.  
    4.     if(time > 0){
    5.         //If time is greater than 0, display time left
    6.         tm.text = Mathf.RoundToInt(time).ToString();
    7.     }else{
    8.         //Otherwise display this
    9.         tm.text = "Time is up!";
    10.     }
    11.    
    12. }
     
  3. Neuronaut12

    Neuronaut12

    Joined:
    Aug 4, 2015
    Posts:
    23
    Awesome, thank you very much!

    As an aside, do you have any advice for delaying the countdown until prompted to begin again? Another script will be responsible for triggering this. Again, thanks in advance!
     
  4. Elmdran

    Elmdran

    Joined:
    Oct 28, 2014
    Posts:
    34
    Do you mean a means to restart the timer, or to pause it and resume it when you wish?
     
  5. Neuronaut12

    Neuronaut12

    Joined:
    Aug 4, 2015
    Posts:
    23
    To pause and resume it
     
  6. Elmdran

    Elmdran

    Joined:
    Oct 28, 2014
    Posts:
    34
    Code (CSharp):
    1. bool paused = false;
    2.  
    3. void ToggleTimer(){
    4.     paused = !paused;
    5. }
    6.  
    7. void Update () {
    8.     if(!paused){
    9.     //If not paused
    10.         if(time > 0){
    11.         //If time is greater than 0, display time left and reduce time
    12.         time -= Time.deltaTime;
    13.         tm.text = Mathf.RoundToInt(time).ToString();
    14.         }else{
    15.         //Otherwise display this
    16.         tm.text = "Time is up!";
    17.         }
    18.     }else{
    19.         //If paused, don't reduce time and display a paused message
    20.         tm.text = "Timer paused";
    21.     }
    22. }
    You would have to use some way to run the ToggleTimer(); function, maybe pressing a certain key? For R for example:

    Code (CSharp):
    1. if(Input.GetKeyDown(KeyCode.R)){
    2.     ToggleTimer();
    3. }
     
  7. Neuronaut12

    Neuronaut12

    Joined:
    Aug 4, 2015
    Posts:
    23
    I'm getting a few errors. Could you please copy/paste the whole code just to make sure I'm not missing something? Thanks :)
     
  8. Elmdran

    Elmdran

    Joined:
    Oct 28, 2014
    Posts:
    34
    Just paste the errors instead and I'll help you sort them out :)
     
  9. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,744
    One side note: Rather than using Mathf.RoundToInt, I recommend learning to use string.Format, ToString(string), and string formatting codes in general - this is exactly what they're made for, they're usually more efficient, and you'll get a lot more versatility.
     
  10. Elmdran

    Elmdran

    Joined:
    Oct 28, 2014
    Posts:
    34
    As someone who is somewhere in between beginner/intermediate in programming, how would int.Parse rank in terms of efficiency?
     
  11. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,744
    It's.... not related? int.Parse turns a string to a number, you're turning from a number to a string.
     
  12. Elmdran

    Elmdran

    Joined:
    Oct 28, 2014
    Posts:
    34
    Yes, you are quite right. I was off somewhere, mentally.
     
  13. Neuronaut12

    Neuronaut12

    Joined:
    Aug 4, 2015
    Posts:
    23
    Well whenever I press R while previewing, nothing happens, it just keeps counting down. As for the GetKeyDown if statement, is this where I would reference the other script that triggers either the countdown or pause?