Nikola Brežnjak blog - Tackling software development with a dose of humor
  • Home
  • Daily Thoughts
  • Ionic
  • Stack Overflow
  • Books
  • About me
Home
Daily Thoughts
Ionic
Stack Overflow
Books
About me
  • Home
  • Daily Thoughts
  • Ionic
  • Stack Overflow
  • Books
  • About me
Nikola Brežnjak blog - Tackling software development with a dose of humor
CodeProject, Unity3D

How to create a countdown timer and show current time in Unity3D

In this post I’m going to show you 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:
    #pragma strict
     
     var timer: float = 70;
     var isFinishedLevel : boolean = false;
     public var displayText : UnityEngine.UI.Text;
     public var timeText : UnityEngine.UI.Text;
     
     var minsDisplay : String;
     var secsDisplay : String;
     
     var mySeconds : int = 0;
     
     private var oldTimer : float;
     
     function Start(){
         oldTimer = timer;
     }
     
     function Update()
     {
         if (!isFinishedLevel) {
             timer -= Time.deltaTime;
         } 
         
         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;
         
         if(mySeconds != s)
         {
             mySeconds = s;
             Timing();
         }
         
     }
     
     function Timing()
     {
         if (timer > 0) {
             //var minsDisplay : String = parseInt( timer / 60 ).ToString();
             minsDisplay = parseInt( timer / 60 ).ToString();
             
             //var secsDisplay : String = parseInt( timer ).ToString();
             secsDisplay = 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;
         }
         displayText.text = minsDisplay + " : " + secsDisplay;
     }
  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 [edit: this is now also fixed and the code is updated with the fix]
  17. If someone has info on how to solve this, please comment and I’ll update the post once the solution is found [edit: both issues have been resolved thanks to Unity Answers, I’m just leaving them here for some future reference].

Recent posts

  • Discipline is also a talent
  • Play for the fun of it
  • The importance of failing
  • A fresh start
  • Perseverance

Categories

  • Android (3)
  • Books (114)
    • Programming (22)
  • CodeProject (35)
  • Daily Thoughts (77)
  • Go (3)
  • iOS (5)
  • JavaScript (127)
    • Angular (4)
    • Angular 2 (3)
    • Ionic (61)
    • Ionic2 (2)
    • Ionic3 (8)
    • MEAN (3)
    • NodeJS (27)
    • Phaser (1)
    • React (1)
    • Three.js (1)
    • Vue.js (2)
  • Leadership (1)
  • Meetups (8)
  • Miscellaneou$ (77)
    • Breaking News (8)
    • CodeSchool (2)
    • Hacker Games (3)
    • Pluralsight (7)
    • Projects (2)
    • Sublime Text (2)
  • PHP (6)
  • Quick tips (40)
  • Servers (8)
    • Heroku (1)
    • Linux (3)
  • Stack Overflow (81)
  • Unity3D (9)
  • Windows (8)
    • C# (2)
    • WPF (3)
  • Wordpress (2)

"There's no short-term solution for a long-term result." ~ Greg Plitt

"Everything around you that you call life was made up by people that were no smarter than you." ~ S. Jobs

"Hard work beats talent when talent doesn't work hard." ~ Tim Notke

© since 2016 - Nikola Brežnjak