Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 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 /
avatar image
0
Question by bulldogpancake · Aug 29, 2020 at 02:51 AM · timetimercoroutinescountdown

Coroutine Countdown Timer being extremely slow

Hey guys so just as a disclaimer I'm relatively new to programming so if I'm making some super obvious mistake please go easy on me

So I'm trying to create a higher customizable Countdown timer for my game, and I want it to be able to be accurate to 0.01 Seconds. I decided I would use the Coroutine method for creating my timer instead of the delta-time one I have seen a couple of times, thinking that this would be a more efficient approach. My game is not very intensive and thus easily runs on hundreds of frames per second, so I thought that using Waitforseconds(0.01) is going to work better because it only needs to be called 100 times every second rather than multiple hundreds. however, I have come into a major issue with my timer. It is EXTREMELY slow. I ran the countdown timer on google and mine side by side starting at 25 seconds and it beat mine out by ten seconds. I even tried adding a artifical delay thinking the waitforseconds function was overshooting, so I would have the time tick down 0.01 seconds when a bit less then that had passed, but my results ended up being sort of inconsistent. Here is my code

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 public class TimerScript : MonoBehaviour
 {
   public Text textDisplay;
   private double secondsLeft = 30;
   public bool takingAway = false;
 
    private string Texttodisplay;
    
 
    public int Milisecondsdigits = 2;
   void Start()
   {
       textDisplay = GetComponent<Text>();
       Texttodisplay = "00:" + secondsLeft;
       
       if(Milisecondsdigits == 0)
       {
           Milisecondsdigits = -1;
       }
   }
 
     void Update()
     {
         if (takingAway == false && secondsLeft > 0)
         {
             StopAllCoroutines();
             StartCoroutine(TimerTake());
         }
         
         if(Texttodisplay.Length > 8 - (Mathf.Abs(Milisecondsdigits -2)))
         {
             Texttodisplay = Texttodisplay.Substring(0,8- (Mathf.Abs(Milisecondsdigits -2)));
         }
 
         textDisplay.text = Texttodisplay;
     }
 
     IEnumerator TimerTake()
     {
         takingAway = true;
         yield return new WaitForSeconds(0.01f);
         secondsLeft -= 0.01;
         if(secondsLeft < 10)
         {
             Texttodisplay = "00:0" + secondsLeft;
 
         }
         
 
         else 
         {
              Texttodisplay = "00:" + secondsLeft;
 
         }
 
         takingAway = false;
     }
 
 }
 

could somebody please let me know how I could cause this to become more accurate or why it's acting extremely inaccurate currently :/

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by YasanthaPrabath · Aug 30, 2020 at 01:55 AM

If you want to have higher accurate Timer I suggest using update as its much faster than doing in coroutine. Starting coroutines have a cost.


On top of that starting, a new instance of coroutine in every update is a bad approach. Pick one, Either use fully coroutine approach or all in Update() approach.


Additionally try to use stringValue.ToString( "FormatString" ) for formatting which is less garbage generating.

https://docs.microsoft.com/en-us/dotnet/standard/base-types/how-to-pad-a-number-with-leading-zeros


Read more about string and Garbage, https://answers.unity.com/questions/1334070/how-to-avoid-garbage-with-strings.html


See below my approach for similar work,

 public class CountDownTimer : MonoBehaviour
 {
     [SerializeField]
     private Text _TextDisplay;
 
     public void StartCountDown( float seconds )
     { 
         StopCoroutine("IEnuCountDown");
         StartCoroutine(IEnuCountDown(_TextDisplay, seconds ));
     }
 
     IEnumerator IEnuCountDown( Text textBox, float countDown )
     {
         float timeLeft = countDown;
 
         while( timeLeft > 0f )
         {
             yield return new WaitForEndOfFrame();
             timeLeft -= Time.deltaTime;
 
             if( timeLeft < 0 )
             {
                 timeLeft = 0;
                 textBox.text = timeLeft.ToString( "00.##" );
                 break;
             }
             else
             {
                 textBox.text = "00.00";
             }
         }
 
         yield return 1;
     }
 
 #if UNITY_EDITOR
     /// <summary>
     /// https://docs.unity3d.com/ScriptReference/ContextMenu.html
     /// </summary>
     [ContextMenu("Test Me")]
     public void TestMe()
     { 
         StartCountDown( 10f );
     }
 #endif
 }



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

137 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

Related Questions

how to make a varied countdown timer 1 Answer

Timer doesn't work properly 2 Answers

CountDown Timer Help (Seconds problem) 2 Answers

Format String Minutes:Seconds 3 Answers

Time does not start counting down when need to 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