{"id":1178,"date":"2015-02-01T14:31:01","date_gmt":"2015-02-01T14:31:01","guid":{"rendered":"http:\/\/www.nikola-breznjak.com\/blog\/?p=1178"},"modified":"2015-08-10T06:54:22","modified_gmt":"2015-08-10T06:54:22","slug":"how-to-create-a-countdown-timer-and-show-current-time-in-unity3d","status":"publish","type":"post","link":"https:\/\/nikola-breznjak.com\/blog\/codeproject\/how-to-create-a-countdown-timer-and-show-current-time-in-unity3d\/","title":{"rendered":"How to create a countdown timer and show current time in Unity3D"},"content":{"rendered":"<p>In this post I&#8217;m going to show you how to create a countdown timer and show current time in Unity3D.<\/p>\n<p><a href=\"http:\/\/www.nikola-breznjak.com\/blog\/wp-content\/uploads\/2015\/02\/TimerTime.jpg\" rel=\"lightbox[1178]\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1193\" src=\"http:\/\/www.nikola-breznjak.com\/blog\/wp-content\/uploads\/2015\/02\/TimerTime.jpg\" alt=\"TimerTime\" width=\"604\" height=\"270\" srcset=\"https:\/\/nikola-breznjak.com\/blog\/wp-content\/uploads\/2015\/02\/TimerTime.jpg 604w, https:\/\/nikola-breznjak.com\/blog\/wp-content\/uploads\/2015\/02\/TimerTime-300x134.jpg 300w\" sizes=\"auto, (max-width: 604px) 100vw, 604px\" \/><\/a><\/p>\n<p><a href=\"http:\/\/nikola-breznjak.com\/_testings\/Unity\/TimerTime\/\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1180\" src=\"http:\/\/www.nikola-breznjak.com\/blog\/wp-content\/uploads\/2015\/02\/demo.jpg\" rel=\"lightbox[1178]\" alt=\"demo\" width=\"264\" height=\"69\" \/><\/a>\u00a0\u00a0<a href=\"https:\/\/github.com\/Hitman666\/TimerTimeUnity3D\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1181\" src=\"http:\/\/www.nikola-breznjak.com\/blog\/wp-content\/uploads\/2015\/02\/forkMe.jpg\" rel=\"lightbox[1178]\" alt=\"forkMe\" width=\"264\" height=\"69\" \/><\/a><\/p>\n<ol>\n<li>Create a new 2D project:<br \/>\n<a href=\"http:\/\/www.nikola-breznjak.com\/blog\/wp-content\/uploads\/2015\/02\/unityCreateNewProject.jpg\" rel=\"lightbox[1178]\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1179\" src=\"http:\/\/www.nikola-breznjak.com\/blog\/wp-content\/uploads\/2015\/02\/unityCreateNewProject.jpg\" alt=\"unityCreateNewProject\" width=\"566\" height=\"359\" srcset=\"https:\/\/nikola-breznjak.com\/blog\/wp-content\/uploads\/2015\/02\/unityCreateNewProject.jpg 566w, https:\/\/nikola-breznjak.com\/blog\/wp-content\/uploads\/2015\/02\/unityCreateNewProject-300x190.jpg 300w\" sizes=\"auto, (max-width: 566px) 100vw, 566px\" \/><\/a><\/li>\n<li>Optionally download some background image (I used <a href=\"http:\/\/i.imgur.com\/Uwy8J.jpg\" rel=\"lightbox[1178]\">this one<\/a>) and drag it to the <strong>Assets<\/strong> folder:<br \/>\n<a href=\"http:\/\/www.nikola-breznjak.com\/blog\/wp-content\/uploads\/2015\/02\/assetsFolder.jpg\" rel=\"lightbox[1178]\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1182\" src=\"http:\/\/www.nikola-breznjak.com\/blog\/wp-content\/uploads\/2015\/02\/assetsFolder.jpg\" alt=\"assetsFolder\" width=\"408\" height=\"161\" srcset=\"https:\/\/nikola-breznjak.com\/blog\/wp-content\/uploads\/2015\/02\/assetsFolder.jpg 408w, https:\/\/nikola-breznjak.com\/blog\/wp-content\/uploads\/2015\/02\/assetsFolder-300x118.jpg 300w\" sizes=\"auto, (max-width: 408px) 100vw, 408px\" \/><\/a><\/li>\n<li>Drag the background image to the <strong>Hierarchy<\/strong>:<strong><br \/>\n<a href=\"http:\/\/www.nikola-breznjak.com\/blog\/wp-content\/uploads\/2015\/02\/Hierarchy.jpg\" rel=\"lightbox[1178]\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1183\" src=\"http:\/\/www.nikola-breznjak.com\/blog\/wp-content\/uploads\/2015\/02\/Hierarchy.jpg\" alt=\"Hierarchy\" width=\"130\" height=\"82\" \/><\/a><br \/>\n<\/strong><\/li>\n<li>Adjust the size of the Camera to 8:<br \/>\n<a href=\"http:\/\/www.nikola-breznjak.com\/blog\/wp-content\/uploads\/2015\/02\/CameraSize.jpg\" rel=\"lightbox[1178]\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1184\" src=\"http:\/\/www.nikola-breznjak.com\/blog\/wp-content\/uploads\/2015\/02\/CameraSize.jpg\" alt=\"CameraSize\" width=\"536\" height=\"277\" srcset=\"https:\/\/nikola-breznjak.com\/blog\/wp-content\/uploads\/2015\/02\/CameraSize.jpg 536w, https:\/\/nikola-breznjak.com\/blog\/wp-content\/uploads\/2015\/02\/CameraSize-300x155.jpg 300w\" sizes=\"auto, (max-width: 536px) 100vw, 536px\" \/><\/a><\/li>\n<li>Hierarchy-&gt;Create-&gt;UI-&gt;Text:<br \/>\n<a href=\"http:\/\/www.nikola-breznjak.com\/blog\/wp-content\/uploads\/2015\/02\/AddingText.jpg\" rel=\"lightbox[1178]\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1185\" src=\"http:\/\/www.nikola-breznjak.com\/blog\/wp-content\/uploads\/2015\/02\/AddingText.jpg\" alt=\"AddingText\" width=\"391\" height=\"424\" srcset=\"https:\/\/nikola-breznjak.com\/blog\/wp-content\/uploads\/2015\/02\/AddingText.jpg 391w, https:\/\/nikola-breznjak.com\/blog\/wp-content\/uploads\/2015\/02\/AddingText-276x300.jpg 276w\" sizes=\"auto, (max-width: 391px) 100vw, 391px\" \/><\/a><\/li>\n<li>Click on Canvas in Hierarchy and set the Render mode:<br \/>\n<a href=\"http:\/\/www.nikola-breznjak.com\/blog\/wp-content\/uploads\/2015\/02\/CanvasRenderMode.jpg\" rel=\"lightbox[1178]\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1186\" src=\"http:\/\/www.nikola-breznjak.com\/blog\/wp-content\/uploads\/2015\/02\/CanvasRenderMode.jpg\" alt=\"CanvasRenderMode\" width=\"542\" height=\"142\" srcset=\"https:\/\/nikola-breznjak.com\/blog\/wp-content\/uploads\/2015\/02\/CanvasRenderMode.jpg 542w, https:\/\/nikola-breznjak.com\/blog\/wp-content\/uploads\/2015\/02\/CanvasRenderMode-300x78.jpg 300w\" sizes=\"auto, (max-width: 542px) 100vw, 542px\" \/><\/a><\/li>\n<li>Click on Text and change the settings (Positions, Width, Height, Text, Font Site):<br \/>\n<a href=\"http:\/\/www.nikola-breznjak.com\/blog\/wp-content\/uploads\/2015\/02\/textSettings.jpg\" rel=\"lightbox[1178]\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1187\" src=\"http:\/\/www.nikola-breznjak.com\/blog\/wp-content\/uploads\/2015\/02\/textSettings.jpg\" alt=\"textSettings\" width=\"545\" height=\"432\" srcset=\"https:\/\/nikola-breznjak.com\/blog\/wp-content\/uploads\/2015\/02\/textSettings.jpg 545w, https:\/\/nikola-breznjak.com\/blog\/wp-content\/uploads\/2015\/02\/textSettings-300x237.jpg 300w\" sizes=\"auto, (max-width: 545px) 100vw, 545px\" \/><\/a><\/li>\n<li>Rename the Text to TimerText<\/li>\n<li>Duplicate the TimerText and rename to TimeText:<br \/>\n<a href=\"http:\/\/www.nikola-breznjak.com\/blog\/wp-content\/uploads\/2015\/02\/duplicate.png\" rel=\"lightbox[1178]\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1188\" src=\"http:\/\/www.nikola-breznjak.com\/blog\/wp-content\/uploads\/2015\/02\/duplicate.png\" alt=\"duplicate\" width=\"384\" height=\"442\" srcset=\"https:\/\/nikola-breznjak.com\/blog\/wp-content\/uploads\/2015\/02\/duplicate.png 384w, https:\/\/nikola-breznjak.com\/blog\/wp-content\/uploads\/2015\/02\/duplicate-260x300.png 260w\" sizes=\"auto, (max-width: 384px) 100vw, 384px\" \/><\/a><\/li>\n<li>Change y position of TimeText to -200:<a href=\"http:\/\/www.nikola-breznjak.com\/blog\/wp-content\/uploads\/2015\/02\/positionChange.jpg\" rel=\"lightbox[1178]\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-large wp-image-1189\" src=\"http:\/\/www.nikola-breznjak.com\/blog\/wp-content\/uploads\/2015\/02\/positionChange-1024x280.jpg\" alt=\"positionChange\" width=\"604\" height=\"165\" srcset=\"https:\/\/nikola-breznjak.com\/blog\/wp-content\/uploads\/2015\/02\/positionChange-1024x280.jpg 1024w, https:\/\/nikola-breznjak.com\/blog\/wp-content\/uploads\/2015\/02\/positionChange-300x82.jpg 300w, https:\/\/nikola-breznjak.com\/blog\/wp-content\/uploads\/2015\/02\/positionChange.jpg 1910w\" sizes=\"auto, (max-width: 604px) 100vw, 604px\" \/><\/a><\/li>\n<li>Hierarchy -&gt; Create -&gt; Create Empty:<br \/>\n<a href=\"http:\/\/www.nikola-breznjak.com\/blog\/wp-content\/uploads\/2015\/02\/CreateEmpty.jpg\" rel=\"lightbox[1178]\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1190\" src=\"http:\/\/www.nikola-breznjak.com\/blog\/wp-content\/uploads\/2015\/02\/CreateEmpty.jpg\" alt=\"CreateEmpty\" width=\"206\" height=\"250\" \/><\/a><\/li>\n<li>Rename it to GameController<\/li>\n<li>Inspector -&gt; Add Component -&gt; New Script (name it Timer and select JavaScript):<br \/>\n<a href=\"http:\/\/www.nikola-breznjak.com\/blog\/wp-content\/uploads\/2015\/02\/AddScript.jpg\" rel=\"lightbox[1178]\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1191\" src=\"http:\/\/www.nikola-breznjak.com\/blog\/wp-content\/uploads\/2015\/02\/AddScript.jpg\" alt=\"AddScript\" width=\"482\" height=\"290\" srcset=\"https:\/\/nikola-breznjak.com\/blog\/wp-content\/uploads\/2015\/02\/AddScript.jpg 482w, https:\/\/nikola-breznjak.com\/blog\/wp-content\/uploads\/2015\/02\/AddScript-300x180.jpg 300w\" sizes=\"auto, (max-width: 482px) 100vw, 482px\" \/><\/a><\/li>\n<li>Paste the following code:\n<pre class=\"lang:default decode:true\">#pragma strict\r\n \r\n var timer: float = 70;\r\n var isFinishedLevel : boolean = false;\r\n public var displayText : UnityEngine.UI.Text;\r\n public var timeText : UnityEngine.UI.Text;\r\n \r\n var minsDisplay : String;\r\n var secsDisplay : String;\r\n \r\n var mySeconds : int = 0;\r\n \r\n private var oldTimer : float;\r\n \r\n function Start(){\r\n     oldTimer = timer;\r\n }\r\n \r\n function Update()\r\n {\r\n     if (!isFinishedLevel) {\r\n         timer -= Time.deltaTime;\r\n     } \r\n     \r\n     CurrentTime();\r\n }\r\n \r\n function CurrentTime() { \r\n     var dt : System.DateTime = System.DateTime.Now;\r\n     var h : int = dt.Hour; \r\n     var m : int = dt.Minute; \r\n     var s : int = dt.Second;\r\n \r\n     timeText.text = h + \":\" + m + \":\" + s;\r\n     \r\n     if(mySeconds != s)\r\n     {\r\n         mySeconds = s;\r\n         Timing();\r\n     }\r\n     \r\n }\r\n \r\n function Timing()\r\n {\r\n     if (timer &gt; 0) {\r\n         \/\/var minsDisplay : String = parseInt( timer \/ 60 ).ToString();\r\n         minsDisplay = parseInt( timer \/ 60 ).ToString();\r\n         \r\n         \/\/var secsDisplay : String = parseInt( timer ).ToString();\r\n         secsDisplay = parseInt( timer ).ToString();\r\n          \r\n         if ( (timer - ( parseInt(minsDisplay) * 60)) &gt; 10 ) {\r\n              secsDisplay = parseInt( timer - ( parseInt(minsDisplay) * 60) ).ToString();\r\n         } \r\n         else {\r\n             secsDisplay = \"0\" + parseInt( timer - ( parseInt(minsDisplay) * 60) ).ToString();\r\n         }\r\n         \r\n         \/\/displayText.text = minsDisplay + \" : \" + secsDisplay;\r\n     } \r\n     else {\r\n          timer += oldTimer;\r\n     }\r\n     displayText.text = minsDisplay + \" : \" + secsDisplay;\r\n }<\/pre>\n<\/li>\n<li>Drag TimerText and TimeText to the script from Hierarchy:<br \/>\n<a href=\"http:\/\/www.nikola-breznjak.com\/blog\/wp-content\/uploads\/2015\/02\/dragging.jpg\" rel=\"lightbox[1178]\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-large wp-image-1192\" src=\"http:\/\/www.nikola-breznjak.com\/blog\/wp-content\/uploads\/2015\/02\/dragging.jpg\" alt=\"dragging\" width=\"485\" height=\"285\" srcset=\"https:\/\/nikola-breznjak.com\/blog\/wp-content\/uploads\/2015\/02\/dragging.jpg 485w, https:\/\/nikola-breznjak.com\/blog\/wp-content\/uploads\/2015\/02\/dragging-300x176.jpg 300w\" sizes=\"auto, (max-width: 485px) 100vw, 485px\" \/><\/a><\/li>\n<li>[<strong>le problems<\/strong>] &#8211; Run the program, and you&#8217;ll run into few issues:\n<ol>\n<li>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 [<strong>edit:<\/strong>\u00a0this seems to be expected behavior and in order to &#8220;fix&#8221; this, you have to set the &#8220;Edit -&gt; Project Settings -&gt; Player -&gt; Run In Background&#8221; option (from <a href=\"http:\/\/answers.unity3d.com\/questions\/890924\/two-timers-out-of-sync.html\">here<\/a>)]<\/li>\n<li>TimerText and TimeText are not &#8220;ticking off&#8221; at the same time [<strong>edit:<\/strong>\u00a0this is now also fixed and the code is updated with the fix]<\/li>\n<\/ol>\n<\/li>\n<li>If someone has info on how to solve this, please comment and I&#8217;ll update the post once the solution is found [<strong>edit:<\/strong>\u00a0both issues have been resolved thanks to <a href=\"http:\/\/answers.unity3d.com\/questions\/890924\/two-timers-out-of-sync.html\">Unity Answers<\/a>, I&#8217;m just leaving them here for some future reference].<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>In this post I&#8217;m going to show you how to create a countdown timer and show current time in Unity3D. \u00a0\u00a0 Create a new 2D project: Optionally download&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8,41],"tags":[],"class_list":["post-1178","post","type-post","status-publish","format-standard","hentry","category-codeproject","category-unity3d"],"_links":{"self":[{"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/posts\/1178","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/comments?post=1178"}],"version-history":[{"count":7,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/posts\/1178\/revisions"}],"predecessor-version":[{"id":2026,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/posts\/1178\/revisions\/2026"}],"wp:attachment":[{"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/media?parent=1178"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/categories?post=1178"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/tags?post=1178"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}