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 Jonathan07 · Mar 13, 2014 at 02:43 AM · timercountdownclock

Timer script not perfectly working

Hi guys, I have a script that makes a countdown timer but I have a problem with it. Here's my code: Variable declaration:

 public float Seconds;
 public float Minutes;

In Update():

         if(Seconds <= 0){
             Seconds = 59;
             if(Minutes >= 1){
                 Minutes --;
             }
             else{
                 Minutes = 0;
                 Seconds = 0;
             }
         }
         else{
             Seconds -= Time.deltaTime;
             if(Mathf.Round (Seconds) <= 9){
                 timerText.text = timerText.text = "Mission time: " + Minutes.ToString () + ":0" + Seconds.ToString("f1");
             }
             else{
                 timerText.text = timerText.text = "Mission time: " + Minutes.ToString () + ":" + Seconds.ToString("f1");
             }
         }

Now, my problem with this code is the transitions. When it reaches 1:01 instead of going to 0:59 it goes from 1:00 to 0:59 to 0:58 really fast since it does the 1:00 for half a second then 0:59 the other half.

Also, when it reaches 0:10, instead of going directly to 0:09.xx it goes 0:9.xx for a few milliseconds.

Anyone can help me fix those two issues? Or is my code just to scrap and redo anew for what I want it to do?

Comment
Add comment · Show 4
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 Jonathan07 · Mar 13, 2014 at 02:49 AM 0
Share

Well I got my problem almost solved for the 0:10 to 0:09 but it still takes a frame to switch and it's visible... Any way to make it instant?

I changed if($$anonymous$$athf.Round (Seconds) <= 9) to if(Seconds <= 9.99f)

avatar image SirCrazyNugget · Mar 13, 2014 at 02:53 AM 0
Share

try using $$anonymous$$athf.Floor

avatar image Jonathan07 · Mar 13, 2014 at 11:11 PM 0
Share

Still getting that frame where it switch :s but that's going to have to do it...

But the problem is still in the $$anonymous$$ute decremental. That takes place on at least half a second.

avatar image VioKyma · Mar 15, 2014 at 01:00 AM 0
Share

I think this may have more to do with the fact that Update is unpredicably called. As in, Update is arbitrarily called as frequently as possible. Have you tried using FixedUpdate?

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by zharik86 · Mar 14, 2014 at 08:41 PM

I will write for you a small example of the timer. The main thing - attention to the string.Format function:

  public float min = 1; //other value
  public float sec = 25; //value from 0 to 59
  private string texttimer = "";

  void Start() {
   texttimer = string.Format("{0:00}:{1:00}", min, sec);
   //here, for example, 0:00, where 0 - number value of list argument - min
   //and 00 - count sign of value
  }

  void Update() {
   sec = sec - Time.deltaTime;
   if(sec < 0) {
    min = min - 1.0f;
    sec = 59.0f;
   }
   if(min >= 0) { //time is over?
    texttimer = string.Format("{0:00}:{1:00}", min, sec);
   }
  }
Comment
Add comment · Show 2 · 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 Jonathan07 · Mar 15, 2014 at 12:52 AM 0
Share

That's good but it still does the same thing :s

Is there no way in unity to have a perfect timer that properly changes from $$anonymous$$utes to seconds or is it that I put the text update at the wrong place? I put it right after with a Debug.Log(texttimer);

avatar image zharik86 · Mar 15, 2014 at 08:22 AM 0
Share

@Jonathan07 Not really clearly that at you happens to transitions. Probably, it is worth trying a little changed option. In it I do counting only on seconds so it shows the true change in seconds:

  public int $$anonymous$$ = 1; //value in $$anonymous$$ute
  public int sec = 25; //value in sec
  private float fullsec = 0.0f;
  private string texttimer = "";
  private string texttimersec = "";

  void Start() {
   fullsec = 60.0f * $$anonymous$$ + sec;
   texttimer = string.Format("{0:00}:{1:00}", $$anonymous$$, sec);
   texttimersec = string.Format("{0:00}", fullsec);
  }
 
  void Update() {
   fullsec = fullsec - Time.deltaTime;
   if(fullsec >= 0) { //time is over?
    int my$$anonymous$$in = (int)(fullsec/60.0f); //current $$anonymous$$ute
    texttimer = string.Format("{0:00}:{1:00}", my$$anonymous$$in, fullsec - my$$anonymous$$in * 60.0f);
    texttimersec = string.Format("{0:00}", fullsec);
   }
  }

But you remember that the string.Format function rounds all values according to rules of mathematics. I.e. value 0.45 is rounded as 0, and value 0.65 is rounded as 1. If you don't want to use rounding, it is possible to discard values after a comma. Thus, and for seconds it is possible to use the int type. However, there is one more method. This creation of function and call of this function through a certain interval of time, in your case 1 second. If the example is necessary, write.

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

22 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

Related Questions

Simple Timer 2 Answers

Calling methods 0 Answers

Timer doesn't work properly 2 Answers

When Score goes up by 25 change timer value 1 Answer

Countdown Through a Label? 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