Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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
0
Question by _C1 · Apr 21, 2016 at 04:05 PM · timetimercountdown

20 minute countdown timer

Hey guys, I have a countdown timer but am unsure of how to make it twenty minutes? I would appreciate any help. Thanks :)

public Text timerText;

 private float time = 1200;

 void Update() {
     time += Time.deltaTime;

     var minutes = time / 60; 
     var seconds = time - 60;
     var fraction = (time * 100) % 100;

     //update the label value
     timerText.text = string.Format ("{0:00} : {1:00} : {2:000}", minutes, seconds, fraction);
 }

}

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

2 Replies

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

Answer by TBruce · Apr 21, 2016 at 04:28 PM

@_C1

Try the following. It uses less CPU cycles as well.

 public Text timerText;
 private float time = 1200;
 
 void Start ()
 {
     StartCoundownTimer();
 }
 
 void StartCoundownTimer()
 {
     if (timerText != null)
     {
         time = 1200;
         timerText.text = "Time Left: 20:00:000";
         InvokeRepeating("UpdateTimer", 0.0f, 0.01667f);
     }
 }
 
 void UpdateTimer()
 {
     if (timerText != null)
     {
         time -= Time.deltaTime;
         string minutes = Mathf.Floor(time / 60).ToString("00");
         string seconds = (time % 60).ToString("00");
         string fraction = ((time * 100) % 100).ToString("000");
         timerText.text = "Time Left: " + minutes + ":" + seconds + ":" + fraction;
     }
 }

Comment
Add comment · Show 4 · 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 _C1 · Apr 22, 2016 at 08:52 AM 0
Share

Thanks very much that works perfectly :)

avatar image unity_04Q0pdmWCplDYg · Jan 20, 2019 at 09:00 PM 0
Share

How can i check with that if it´s below 0?

avatar image hollym16 unity_04Q0pdmWCplDYg · Jan 23, 2019 at 12:26 PM 0
Share

void Update(){ time -= Time.deltaTime; if(time < 0) { //Do Something }

avatar image calliet · Nov 02, 2020 at 09:06 PM 0
Share

very helpful!

avatar image
-1

Answer by MyrDovg · Oct 30, 2017 at 11:32 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 · 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

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

62 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 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 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 avatar image avatar image avatar image avatar image avatar image

Related Questions

Problem with a stopwatch 0 Answers

How do I do math in unity.(timer/60 and timer%60) 1 Answer

How can I record the time that has passed between mouse clicks? 1 Answer

Unity3d countdown timer is not counting down 0 Answers

Milliseconds Timer Question 1 Answer


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