Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
2
Question by Nocturnal_Glow · Jun 04, 2015 at 02:54 PM · countdown

Count down timer C#

I feel like there is something I'm missing but I don't know why I can't get my text in my canvas to display the time left float value. how do I fix this

CountDown.cs:

 using UnityEngine;
 using UnityEngine.UI;
 using System.Collections;
 
 public class CountDown : MonoBehaviour
 {
     float timeLeft = 300.0f;
 
     Text text;
 
     void Awake ()
     {
         text = GetComponent<text> ();
     }
     
     void Update()
     {
         timeLeft -= Time.deltaTime;
         text.text = "time Left:" + timeLeft;
         if(timeLeft < 0)
         {
             Application.LoadLevel("gameOver");
         }
     }
 }

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

7 Replies

· Add your reply
  • Sort: 
avatar image
11
Best Answer

Answer by Nomabond · Jun 04, 2015 at 03:02 PM

Hi Nocturnal,

On line 13 you are using GetComponent(); but the actual component you are trying to find is a Text (notice the capital 'T' ).

It would be much easier for you though if you set your Text text to a public variable and then use the inspector to drag the reference of your UI Text into the script.

Here is a slightly modified version of your script that you can use to get a nice rounded integer countdown from 300. Let me know if you have any questions about it!

 using UnityEngine;
 using UnityEngine.UI;
 using System.Collections;
 
 public class CountDown : MonoBehaviour
 {
     float timeLeft = 300.0f;
     
     public Text text;
     
 
     
     void Update()
     {
         timeLeft -= Time.deltaTime;
         text.text = "Time Left:" + Mathf.Round(timeLeft);
         if(timeLeft < 0)
         {
             Application.LoadLevel("gameOver");
         }
     }
 }

 
Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Nocturnal_Glow · Jun 04, 2015 at 04:46 PM 1
Share

Yep that worked, thanks so much!

avatar image Rocketman123 · Dec 22, 2016 at 11:46 PM 1
Share

Thanks this worked for me! i changed a little but hey.

avatar image inzi14 · Aug 14, 2018 at 05:08 PM 0
Share

Hey I want to makr text box in which user will put value and after that value sound must play. How to do that plz help. I am new in this field

avatar image
13

Answer by deLord · Oct 03, 2015 at 10:57 AM

I found Nocturnals timer a bit crude, so I came up with this:

 using System.Collections;
 using UnityEngine.UI;
 
 public class Countdown : MonoBehaviour {
 
     public float timeLeft = 300.0f;
     public bool stop = true;
 
     private float minutes;
     private float seconds;
     
     public Text text;
 
     public void startTimer(float from){
         stop = false;
         timeLeft = from;
         Update();
         StartCoroutine(updateCoroutine());
     }
     
     void Update() {
         if(stop) return;
         timeLeft -= Time.deltaTime;
         
         minutes = Mathf.Floor(timeLeft / 60);
         seconds = timeLeft % 60;
         if(seconds > 59) seconds = 59;
         if(minutes < 0) {
             stop = true;
             minutes = 0;
             seconds = 0;
         }
 //        fraction = (timeLeft * 100) % 100;
     }
 
     private IEnumerator updateCoroutine(){
         while(!stop){
             text.text = string.Format("{0:0}:{1:00}", minutes, seconds);
             yield return new WaitForSeconds(0.2f);
         }
     }
 }


Hope this helps as well

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image pravinbudharap · Apr 13, 2017 at 09:35 AM 0
Share

yiepppp thank you so much for this its save my time with counter thanks buddy .!!!

avatar image solloranoharold123 · Mar 14, 2019 at 05:19 PM 0
Share

How about if I want to decrease it by 10 seconds after I click 4 times :) and the count of the click will back as 0. Then the whole process continue as the timer goes 0. HOpe you answer :)

Here's the code.

void Update() { countdownTime -= Time.deltaTime; if (DisplayItems.totalclicks==4) { decreaseTime += 10;

         DisplayItems.totalclicks = 0;
     }

   
      $$anonymous$$utes = $$anonymous$$athf.FloorToInt(countdownTime / 60F);
     seconds = $$anonymous$$athf.FloorToInt(countdownTime - $$anonymous$$utes * 60) -decreaseTime;


   


     string niceTime = string.Format("{0:0}:{1:00}", $$anonymous$$utes, seconds);
     text.text = niceTime;
 }
avatar image unity_bfawXRcvR563OA · Jun 27, 2019 at 02:18 PM 0
Share

How do you check when it reaches 00:00? This is my code, but it won't work. if(($$anonymous$$utes==0) && (seconds == 0)) { stop = true; timer.gameObject.SetActive(false); }

avatar image
-1

Answer by MyrDovg · Oct 30, 2017 at 11:31 AM

         float totalTime = 120f; //2 minutes
 
         private void Update()
         {
             totalTime -= Time.deltaTime;
             UpdateLevelTimer(totalTime );
         }
 
         public void UpdateLevelTimer(float totalSeconds)
         {
             int minutes = Mathf.FloorToInt(totalSeconds / 60f);
             int seconds = Mathf.RoundToInt(totalSeconds % 60f);
 
             string formatedSeconds = seconds.ToString();
 
             if (seconds == 60)
             {
                 seconds = 0;
                 minutes += 1;
             }
 
             timer.text = minutes.ToString("00") + ":" + seconds.ToString("00");
         }

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Garazbolg · Oct 30, 2017 at 02:44 PM 0
Share

Necropost and wrong on many levels :

  • You can't modulo a float

  • If you use a modulo correctly your whole if(seconds == 60) block is useless

  • You don't use your formatedSeconds

  • No reference to timer

  • with your $$anonymous$$athf.RoundToInt your actual totalTime will hit zero 0.5s after your text did, what ?!?

And that's not even the worst part : You've been copy and pasting this clumsy code on multiple questions that where already answered.

Just dont do that, please ...

avatar image
-1

Answer by creatorOFgravityracer · Dec 09, 2017 at 08:38 AM

GravityGamezProd

HERES THE FIXZ TO ALL WHO IS STILL LOOKING AT THIS COUNTDOWN SCRIPT ON UNITY 5

*TODAYS DATE 12/9/17 YOUR WELCOME

using UnityEngine; using UnityEngine.UI; using System.Collections;

public class CountDown : MonoBehaviour { public float timeLeft = 300.0f;

 Text text;

 void Awake ()
 {
     text = GetComponent<Text> ();
 }

 void Update()
 {
     timeLeft -= Time.deltaTime;
     text.text = "time Left:" + timeLeft;
     if(timeLeft < 0)
     {
         Application.LoadLevel("gameOver");
     }
 }

}

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by U_Ku_Shu · Nov 25, 2016 at 10:09 AM

 float _countDownTimeLeft = 30.0f;
 bool _timerIsGoing;
 void Update()
 {
     UpdCountDown();
 }

 private void UpdCountDown()
 {
     if (_timerIsGoing && _countDownTimeLeft > 0)
     {
         _countDownTimeLeft -= Time.deltaTime;
     }
 }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
  • 1
  • 2
  • ›

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

19 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

321 Countdown Coroutine stuck on 3 0 Answers

How to make a countdown timer in unity that is running even before user installs app(and in background) 0 Answers

Hi there i´m new to unity and like to solve a problem: i like to run the timer/countdown backwards from 15-0 and after this going to an game over screen 0 Answers

I've been trying to write for a plane to get smaller over time in 3D, i couldn't figure it out,i've been trying to write a code for a plane to get smaller over time, i couldn't figure it out! 0 Answers

Timer Countdown Pop Up Screen Before Scene "Starts" Please Help!! 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges