Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

My timer is glitchy.

Discussion in 'Getting Started' started by Petras99, Jun 3, 2016.

  1. Petras99

    Petras99

    Joined:
    May 29, 2016
    Posts:
    27
    Code (csharp):
    1.  
    2. public Text timer;
    3.  
    4. public float myTimer = 0.0f;
    5.  
    6. myTimer += Time.deltaTime;
    7.  
    8.             int minutes = (int)myTimer / 60;
    9.             float seconds = (int)myTimer % 60;
    10.             timer.text = "Time: " + minutes.ToString () + ":" + seconds.ToString ("00") + myTimer.ToString ().PadRight (2).Substring (1, 2);
    11.             //fixes the time to show correctly for  a larger number.
    12.             if (myTimer >= 10.0) {
    13.                 timer.text = "Time: " + minutes.ToString () + ":" + seconds.ToString ("00") + myTimer.ToString ().PadRight (2).Substring (2, 2);
    14.             }
    15.         }
    16.  
    17.  

    This timer converts seconds into minutes, and it works just fine when i only show the integer number and not the float number. I have made it work just fine as a float... Until the time reaches 1 minute and 40 seconds (100 total seconds without the conversion to minutes). I will have pictures to clarify what i am saying..

    upload_2016-6-2_21-7-45.png

    upload_2016-6-2_21-8-49.png

    Note the timer in the bottom left corner. also note the "My Timer" in the inspector, and in the Timer Script.
     
  2. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    20,727
    You're using Time.deltaTime which is giving you the length of time that passed between frames. Anything that causes the frame rate to change will directly affect Time.deltaTime. If you're trying to get the number of seconds then you need to use Time.time which returns the number of seconds since the game started.
     
  3. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    Your code is telling the computer to do weird things, and it's obliging. You say:

    Code (csharp):
    1. timer.text = "Time: " + minutes.ToString() + ":" + seconds.ToString("00") + myTimer.ToString().PadRight (2).Substring (2, 2);
    and your timer value is 103.3587. So the text gets set to "Time" plus the minutes ("1"), plus a colon, plus the seconds to two decimal places ("43"), plus (for reasons I can't imagine) the first third and fourth characters of the time value ("3."). The result is "1:433."

    I have no idea why you're telling it to do that, but as far as I can see it is doing exactly what you said you wanted.
     
  4. Petras99

    Petras99

    Joined:
    May 29, 2016
    Posts:
    27
    I was not aware of that. i will have to fix it.
     
  5. Petras99

    Petras99

    Joined:
    May 29, 2016
    Posts:
    27
    That format was intended for when the seconds is below 100. I have tried other if statements to check if seconds is greater than 100. However i have had no luck. I am not very familiar with C#, so i am not completely aware of how the ToString("00") works. What would you suggest to fix my problem?
     
  6. Petras99

    Petras99

    Joined:
    May 29, 2016
    Posts:
    27
    When i change
    Code (csharp):
    1.  myTimer += Time.deltaTime
    to
    Code (csharp):
    1.  myTimer += Time.time
    it changes abnormally quick. Like 2 minutes per second quick.
     
  7. boolfone

    boolfone

    Joined:
    Oct 2, 2014
    Posts:
    289
    If the time is 99.75, your code picks out the period and the 7 by your Substring call.

    However, if it is 100.75, your code picks out the 0 and the period.

    Your code is basically picking out the third and fourth characters always, but it seems like you want the period and what’s after the period.

    Maybe try something like this:

    Code (CSharp):
    1. myTimer += Time.deltaTime;
    2.  
    3. int minutes = (int)myTimer / 60;
    4. float seconds = (int)myTimer % 60;
    5.  
    6. timer.text = "Time: " + minutes.ToString () + ":" + seconds.ToString ("00") + (myTimer - (int)myTimer).ToString ().Substring (1, 2);
    As far as I can tell, that works.
     
    Petras99 likes this.
  8. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    I still don't understand what you're trying to do. Can you give several examples (covering all the relevant cases) of time value, and the string you want?
     
  9. Petras99

    Petras99

    Joined:
    May 29, 2016
    Posts:
    27
    That works great thank you!
     
  10. Petras99

    Petras99

    Joined:
    May 29, 2016
    Posts:
    27
    I was just trying to have a timer that counted, and continues until the player finishes the course, however when the seconds reached above 100 it drastically changed the format. So the decimal point was 5 characters over. However in an answer above someone has shown me code that works great. I apologize for the confusion and my ignorance.
     
  11. boolfone

    boolfone

    Joined:
    Oct 2, 2014
    Posts:
    289
    Actually, I noticed the code I gave you had a bug that could be triggered if myTimer was exactly a whole number. (i.e. It might try to grab the 2nd and 3rd characters of the string "5")

    This code should solve that issue. Please use this instead:

    Code (CSharp):
    1.         myTimer += Time.deltaTime;
    2.  
    3.         int minutes = (int)myTimer / 60;
    4.         float seconds = (int)myTimer % 60;
    5.  
    6.         timer.text = "Time: " + minutes.ToString () + ":" + seconds.ToString ("00") + "." + ((int)(myTimer * 10f)) % 10;
    Thanks.
     
    Petras99 likes this.
  12. MrUnecht

    MrUnecht

    Joined:
    Jan 16, 2016
    Posts:
    30
    Time.deltaTime is calculated to be 1 Second.
    If you got 10 fps, Time.deltaTime is 0.1f
    Now wait the 10 frames which is 1 second (fps = frames per second)

    So 0,1 * 10 = 1

    So he can use it like he is doong it
     
  13. MrUnecht

    MrUnecht

    Joined:
    Jan 16, 2016
    Posts:
    30
    What does this:
    Code (CSharp):
    1.  
    2. ((int)(myTimer * 10f)) % 10;
    3.  
    If myTimer is 5 then
    (5 * 10f) % 10
    (50f) % 10
    5f

    Looks useless to me...
     
  14. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    That code gets the 10th (i.e. the first digit past the decimal). Your math is wrong — 50 % 10 is 0, not 5.

    I still don't know if that's what @Petras99 actually wants, because he seems unwilling or unable to define what he wants. But that's what this code does, anyway.

    An easier way to do the same thing would be to let the ToString format do it for you:

    Code (CSharp):
    1. timer.text = "Time: " + minutes.ToString() + ":" + seconds.ToString("00.0");
     
    Ryiah likes this.
  15. MrUnecht

    MrUnecht

    Joined:
    Jan 16, 2016
    Posts:
    30
    Ah ok thanks:)
     
  16. Petras99

    Petras99

    Joined:
    May 29, 2016
    Posts:
    27
    I wanted a working timer, that showed 2 points to the left of the decimal point, and one point to the right of the decimal point, which i have now. Again i apologize for any confusion caused and my ignorance upon the subject.
     
  17. Petras99

    Petras99

    Joined:
    May 29, 2016
    Posts:
    27
    Ok will do. Thank you for this! it works great!