Code (csharp): public Text timer; public float myTimer = 0.0f; myTimer += Time.deltaTime; int minutes = (int)myTimer / 60; float seconds = (int)myTimer % 60; timer.text = "Time: " + minutes.ToString () + ":" + seconds.ToString ("00") + myTimer.ToString ().PadRight (2).Substring (1, 2); //fixes the time to show correctly for a larger number. if (myTimer >= 10.0) { timer.text = "Time: " + minutes.ToString () + ":" + seconds.ToString ("00") + myTimer.ToString ().PadRight (2).Substring (2, 2); } } 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.. Note the timer in the bottom left corner. also note the "My Timer" in the inspector, and in the Timer Script.
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.
Your code is telling the computer to do weird things, and it's obliging. You say: Code (csharp): 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.
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?
When i change Code (csharp): myTimer += Time.deltaTime to Code (csharp): myTimer += Time.time it changes abnormally quick. Like 2 minutes per second quick.
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): myTimer += Time.deltaTime; int minutes = (int)myTimer / 60; float seconds = (int)myTimer % 60; timer.text = "Time: " + minutes.ToString () + ":" + seconds.ToString ("00") + (myTimer - (int)myTimer).ToString ().Substring (1, 2); As far as I can tell, that works.
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?
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.
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): myTimer += Time.deltaTime; int minutes = (int)myTimer / 60; float seconds = (int)myTimer % 60; timer.text = "Time: " + minutes.ToString () + ":" + seconds.ToString ("00") + "." + ((int)(myTimer * 10f)) % 10; Thanks.
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
What does this: Code (CSharp): ((int)(myTimer * 10f)) % 10; If myTimer is 5 then (5 * 10f) % 10 (50f) % 10 5f Looks useless to me...
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): timer.text = "Time: " + minutes.ToString() + ":" + seconds.ToString("00.0");
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.