- Home /
Converting float to string with time format; sending variables to disabled game object
I'm working on a C# script which counts and records the amount of time the player has spent in the game from the start of the game to the end. The script uses three major float variables:
timer - used as a temp variable for counting game time
finishTime - time when player finishes game. used for comparison purposes
bestTime - best time player has received when playing the game. Aquired from PlayerPrefs value. Used for comparision purposes
The script in question is supposed to do the following:
count time upwards in seconds
when the game is finished, set the value of the finishedTime to the value of timer
compare bestTime to finishTime: if finishTime > bestTime, bestTime = finishTime
save bestTime using PlayerPrefs
convert finishTime, bestTime into strings using the H:MM:SS format
send strings to game objects with GUIText components to display strings on game over screen
For reference, this is the main block of code giving me trouble:
void SaveBestTime() {
finishTime = timer;
//only save best time if previous time has been surpassed
if (finishTime > bestFinishTime) {
bestFinishTime = finishTime;
PlayerPrefs.SetFloat("bestTime", bestFinishTime);
}
}
Currently, I'm having problems converting the finishTime and bestTime into a H:MM:SS format, as well as sending the strings to the GUIText objects. I'm not sure what to do when converting seconds into minutes and hours. The reason I didn't convert the time when counting it was that I needed the timer value to be set for the finishTime value for comparing between that and the bestTime values. I also would need to convert the bestTime value into a string with the H:MM:SS format regardless of the situation.
The game objects I mentioned before are deactivated when the game starts (i.e. they're grayed out in the Hierarchy), and I'm receiving NullReferenceExceptions. I'm not sure if these facts are related, but I would like some clarification regardless.
If anyone could help me out with both of these issues above, the aid would much appreciate it. Thank you for taking the time to read this question.
Answer by tanoshimi · Dec 20, 2013 at 03:51 PM
To display a float number of seconds, timer, as mm:ss, you could use a string.Format as follows:
string minSec = string.Format("{0}:{1:00}", (int)timer / 60, (int)timer % 60); guiText.text = minSec;
Alternatively, you could use the value to initialise an instance of the TimeSpan class - http://www.dotnetperls.com/timespan - which has various inbuilt methods for dealing with and displaying time values.
I used your first suggestion, and it worked perfectly. Thanks again.
If my rep was better i'd give it the thumbs up ... this answer was very helpful ... I used it in this way ... string $$anonymous$$Sec = string.Format("{0}:{1:00}", (int)Time.timeSinceLevelLoad / 60, (int)Time.timeSinceLevelLoad % 60); guiText.text = $$anonymous$$Sec;
thanks
Your answer
![](https://koobas.hobune.stream/wayback/20220613124546im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
HighScore - Timer 1 Answer
How do you save timer with PlayerPrefs? 1 Answer
Save a timer in Hour:Minutes:Seconds to playerPrefs then load it on start 2 Answers
Timer highscore trouble 1 Answer