Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Team_26 · Sep 16, 2016 at 02:58 PM · c#timetimercountdown

Timer doesn't work properly

I wrote a simple countdown timer for my game. Please have a look on it:

 void Update()
 {
         timer -= Time.deltaTime;
         int mins = (int)timer / 60.0f;
         int seconds = (int)timer % 60.0f;
         text.text = timer.ToString (mins.ToString() + ':' + seconds.ToString()); // Text is a reference to UI Text object
 }

At start I set it to 2 mins (120 seconds). Everything works fine until the timer reach 1:50 - then it displays 1:5110 instead of 1:50. I really don't know why this happens. I tried to add the following line to the script:

 text.text = text.text.Substring(0,4);

But after doing it timer was just avoiding 1:50. There was a 2 second long break between 1:51 and 1:49 . The problem happens every 10 seconds. Can you help me?

Comment
Add comment · Show 3
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 Sinnii · Sep 16, 2016 at 03:42 PM 0
Share

Don't you want:

int $$anonymous$$s = (int)(timer / 60.0f); int seconds = (int)(timer % 60.0f);

As an aside, for performance you probably want to make sure you only run this code section(except timer -= Time.deltaTime) once every second.

avatar image Team_26 Sinnii · Sep 16, 2016 at 03:54 PM 0
Share

This is the same as my code, isn't it? I think it doesn't matter if you add these brackets or not. The timer is still not working ;/ Thanks for reply

avatar image Sinnii Team_26 · Sep 16, 2016 at 04:25 PM 0
Share

Ah sorry, forgot the last line; text.text = $$anonymous$$s.ToString() + ":" + seconds.ToString();

But no, those two lines are not exactly what you're doing. You are converting timer to an int, then dividing by a float, then converting back to int. In fact, my solution is bad too. Best just remove your .0f ins$$anonymous$$d, so it just does a simple int division/modulus ins$$anonymous$$d of the more expensive float version.

2 Replies

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

Answer by m0guz · Sep 16, 2016 at 05:02 PM

This is my code, works fine for me. I used FixedUpdate, you don't have to run timer on every frame. Your problem most likely caused by "60.0f" float numbers. Try adding Mathf.Floor(timer % 60.0f)

     void FixedUpdate()
     {
         timePassed += Time.deltaTime;
         float timeLeft = maxTime - timePassed;
 
         string min = Mathf.Floor(timeLeft / 60).ToString("00");
         string sec = Mathf.Floor(timeLeft % 60).ToString("00");
 
         timeString.text = string.Format("{0}:{1}", min, sec);
     }
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 Team_26 · Sep 16, 2016 at 07:31 PM 0
Share

Quite interesting. I think the last time did the job ;] Thanks so much.

avatar image
1

Answer by NerdClown · Sep 16, 2016 at 05:10 PM

You had me stumped dude!

Added this debug line, and it was never triggered, even if the numbers kept being weird:

   if (seconds > 60)
   {
       Debug.Log("MEH UNEXPECTED FAIL! timer: " + timer + " as int: " + ((int)timer) + " timer mod 60: " + ((int)timer) % 60 + " seconds: " + seconds);
   }
     

So it turned out it was the formatting of the string that I hadn't even bothered looking twice at :)

This works for getting the string version of the time:

 Debug.Log(string.Format("[{0}:{1}]",mins, seconds ));

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 Team_26 · Sep 16, 2016 at 07:31 PM 0
Share

Changing string formatting solved the problem. Thanks for your reply!

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

221 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 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 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 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

countdown timer acivation 1 Answer

C# countdown timer 9 Answers

Timer counting UP instead of DOWN 1 Answer

Why does my MatchTime Class not work when using Properties? 1 Answer

Coroutine Countdown Timer being extremely slow 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