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 /
avatar image
0
Question by gpvisual · Feb 07, 2014 at 05:11 PM · timercountdown

How do I change this countdown timer to M:S

Hi I downloaded this very elegant javascript for a countdown timer which reverts back to menu page when it reaches zero but I can't figure out how to change the countdown from seconds to minutes and seconds i.e. 2 minutes needs to be displayed as 2:00 and not 120. Been looking for ages for an answer so any help would be much appreciated.

 var timerText : GUIText;
 
 var roundTime = 60.0;
 
 var thisSecond;
 
 var lastSecond = 0;
 
 
 
 function Update () {
 
 thisSecond = Mathf.FloorToInt(Time.time);
 
 if(thisSecond > lastSecond)
 
     {
 
         roundTime -= 1;
 
         timerText.text = roundTime.ToString();
 
     }
 
     
 
 if(roundTime < 1)
 
 {
 
     Application.LoadLevel(0);
 
 }
 
 lastSecond = thisSecond;
 
  
 
 }
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
0

Answer by xandermacleod · Feb 08, 2014 at 12:51 AM

to turn seconds into minutes you do 2 operations. First is for the number of minutes, the second is for the remaining seconds.

To get the minutes first divide your total seconds by 60 then do Mathf.Floor on it's result. i.e. var minutes = Mathf.Floor(totalSeconds / 60);

To get the remaining seconds you do a special type of division that returns the remainder. Instead of doing a division (e.g. 120 / 60) use the percentage symbol (i.e. 120 % 60). i.e. var remainingSeconds = totalSeconds % 60;

hope this helps

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 gpvisual · Feb 10, 2014 at 11:33 AM 0
Share

Sorry for being thick but where do I put the lines above. If I add them to the variables at the top I get an error "BCE0005:$$anonymous$$ Identifier:'totalSeconds'. I am guessing I have to modify the remaining code to reflect the changes but am not sufficiently advanced at coding to work out where. I apologise as I am a 3D modeller trying to learn coding rather than a coder trying to learn modelling (which is obviously the easier option).

avatar image gpvisual · Feb 12, 2014 at 08:59 AM 0
Share

Just wondering if anyone knows how to incorporate the suggested code so that it works without returning an error. I added the two lines at the top and am guessing I also have to alter the body of the code to reflect the changes but do not have the skill. If we can get this code working to show $$anonymous$$utes and seconds it is a very simple and easy solution to a countdown timer. I have searched for something better but this is the best I could find so far. If anyone has a better timer to attach to GUI text I would be grateful.

avatar image xandermacleod · Feb 12, 2014 at 09:46 AM 0
Share

I haven't tested it so there might be bugs, but you'll want something like this:

 public var countdownTotal = 120.0;
 public var $$anonymous$$uteText : GUIText;
 public var secondText : GUIText;

 private var secondsLeft : float;
 private var $$anonymous$$utesTextNumber : int;
 private var secondsTextNumber : int;
 
 function Update()
 {
     secondsLeft = countdownTotal - Time.time;
     
     if(secondsLeft <= 0)
     {
         CountdownHasHitZero();
     }
     
     $$anonymous$$utesTextNumber = $$anonymous$$athf.FloorToInt(secondsLeft / 60);
     secondsTextNumber = $$anonymous$$athf.FloorToInt(secondsLeft % 60);
     
     $$anonymous$$uteText.text = $$anonymous$$utesTextNumber.ToString();
     secondText.text = secondsTextNumber.ToString();
 }
 
 function CountdownHasHitZero()
 {
     Application.LoadLevel(0);
 }
avatar image xandermacleod · Feb 12, 2014 at 09:56 AM 0
Share

you might wanna test to see when it reaches 60 seconds if it shows up as 1:00 or 0:00... if it shows up as zero then let me know and ill adjust it.

avatar image
0

Answer by gpvisual · Feb 12, 2014 at 10:18 AM

Actually sorted it myself (or to be correct my son did). If from line 15 the first "if" statement you add the following it works perfectly.

 if(thisSecond > lastSecond)
 
     {
 
         roundTime -= 1;
 minutes = Mathf.FloorToInt(roundTime/60);
 seconds = roundTime % 60;
 timerText.text = minutes + ":" + seconds;
     }
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

20 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

Related Questions

Making a Timer Out out of 3D Text using C#. 1 Answer

How to reset Time.time ? 1 Answer

Timer inexplicably counting down slower after it reaches a certain number. 0 Answers

How to use the same script on different objects at the same time? 1 Answer

advanced countdown timer help please 0 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