Click here to Skip to main content
15,883,901 members
Articles / Programming Languages / Javascript

How to Create a Countdown Timer and Show Current Time in Unity3D

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
1 Feb 2015CPOL1 min read 23.1K   4   2
How to create a Countdown Timer and show current time in Unity3D

TimerTime

demo  forkMe

  1. Create a new 2D project:

    unityCreateNewProject

  2. Optionally, download some background image (I used this one) and drag it to the Assets folder:

    assetsFolder

  3. Drag the background image to the Hierarchy:

    Hierarchy

  4. Adjust the size of the Camera to 8:

    CameraSize

  5. Hierarchy->Create->UI->Text:

    AddingText

  6. Click on Canvas in Hierarchy and set the Render mode:

    CanvasRenderMode

  7. Click on Text and change the settings (Positions, Width, Height, Text, Font Site):

    textSettings

  8. Rename the Text to TimerText.
  9. Duplicate the TimerText and rename to TimeText:

    duplicate

  10. Change y position of TimeText to -200:positionChange
  11. Hierarchy -> Create -> Create Empty:

    CreateEmpty

  12. Rename it to GameController
  13. Inspector -> Add Component -> New Script (name it Timer and select JavaScript):

    AddScript

  14. Paste the following code:
    JavaScript
    #pragma strict
    
    var timer: float = 70;
    var isFinishedLevel : boolean = false;
    public var displayText : UnityEngine.UI.Text;
    public var timeText : UnityEngine.UI.Text;
    
    private var oldTimer : float;
    
    function Start(){
    	oldTimer = timer;
    }
    
    function Update()
    {
    	if (!isFinishedLevel) {
    		timer -= Time.deltaTime;
    	} 
    
    	if (timer > 0) {
    		var minsDisplay : String = parseInt( timer / 60 ).ToString();
    		var secsDisplay : String = parseInt( timer ).ToString();
    		 
    		if ( (timer - ( parseInt(minsDisplay) * 60)) > 10 ) {
    		     secsDisplay = parseInt( timer - ( parseInt(minsDisplay) * 60) ).ToString();
    		} 
    		else {
    			secsDisplay = "0" + parseInt( timer - 
                              ( parseInt(minsDisplay) * 60) ).ToString();
    		}
    		
    		displayText.text = minsDisplay + " : " + secsDisplay;
    	} 
    	else {
    	 	timer = oldTimer;
    	}
    	
    	CurrentTime();
    }
    
    function CurrentTime() { 
    	var dt : System.DateTime = System.DateTime.Now;
    	var h : int = dt.Hour; 
    	var m : int = dt.Minute; 
    	var s : int = dt.Second;
    
    	timeText.text = h + ":" + m + ":" + s;
    }
  15. Drag TimerText and TimeText to the script from Hierarchy:

    dragging

  16. [le problems] – Run the program, and you’ll run into few issues:
    1. If you open up any other window, you will notice that the timer will stop, and continue counting once you return to the Unity window [edit: this seems to be expected behavior and in order to “fix” this, you have to set the “Edit -> Project Settings -> Player -> Run In Background” option (from here)].
    2. TimerText and TimeText are not “ticking off” at the same time.
  17. If someone has information on how to solve this, please comment and I’ll update the post once the solution is found.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
Croatia Croatia
I’m an engineer at heart and a jack of all trades kind of guy.

For those who care about titles, I hold a masters degree in computing from FER (and a black belt in karate, but that’s another story…).

During the last years, worked in a betting software industry where I made use of my knowledge in areas ranging from full-stack (web & desktop) development to game development through Linux and database administration and use of various languages (C#, PHP, JavaScript to name just a few).

Currently, I’m a senior software engineer at TelTech, where we make innovative communications apps, and I <3 it.

Lately, I’m very passionate about Ionic framework and am currently in the top 3 answerers on StackOverflow in Ionic framework. I wrote a book about Ionic framework which you can get for free on Leanpub: Ionic framework – step by step from idea through prototyping to the app stores.

Other technical writing:

+ wrote a book Getting MEAN with MEMEs
was a technical reviewer for a book Deploying Node published by Packt
was a technical reviewer for a book Getting started with Ionic published by Packt
After writing 300 posts, this is why I think you should start blogging too

Come and see what I write about on my blog.

Comments and Discussions

 
QuestionKeeping countdown in sync with the system time Pin
Austin Mullins3-Feb-15 11:33
Austin Mullins3-Feb-15 11:33 
AnswerRe: Keeping countdown in sync with the system time Pin
Nikola Breznjak3-Feb-15 19:45
professionalNikola Breznjak3-Feb-15 19:45 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.