Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Converting float to H:MM:SS format, sending variables to disabled game object

Discussion in 'Scripting' started by Rxanadu, Dec 20, 2013.

  1. Rxanadu

    Rxanadu

    Joined:
    May 24, 2012
    Posts:
    50
    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:
    Code (csharp):
    1. void SaveBestTime() {
    2.        
    3.         finishTime = timer;
    4.  
    5.         //only save best time if previous time has been surpassed
    6.         if (finishTime > bestFinishTime) {
    7.             bestFinishTime = finishTime;
    8.             PlayerPrefs.SetFloat("bestTime", bestFinishTime);
    9.         }
    10.     }
    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 post.
     
  2. TheCheese

    TheCheese

    Joined:
    Nov 25, 2009
    Posts:
    82
    You want to look at the TimeSpan .net class:
    http://msdn.microsoft.com/en-us/library/system.timespan.fromseconds(v=vs.110).aspx

    Basically you can do this:
    System.TimeSpan interval = System.TimeSpan.FromSeconds( seconds );

    Then to format that into a string you can simply do this:
    String timeString = interval .Hours + ":" + interval .Minutes + ":" + interval.Seconds;

    You could also fight with the TimeSpan Format Strings if you wanted more control over the string format, although I personally find that rather confusing.
    http://msdn.microsoft.com/en-us/library/ee372287(v=vs.110).aspx

    The NullReferenceException is happening because the GameObject is deactivated. I believe you can access scripts attached to the gameObject when the gameObject is not active, but you cannot access the gameObject itself. When hiding objects I usually turn off the renderer instead - or move it off the screen somewhere until needed.