Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Rxanadu · Dec 20, 2013 at 02:37 PM · c#nullreferenceexceptionplayerprefstimer

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.

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
8
Best Answer

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.

Comment
Add comment · Show 3 · 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
avatar image Rxanadu · Dec 21, 2013 at 06:59 PM 0
Share

I used your first suggestion, and it worked perfectly. Thanks again.

avatar image suiteazabug · Jan 17, 2014 at 05:20 AM 0
Share

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

avatar image navi2121 · Jan 01, 2016 at 06:00 PM 0
Share

wow, cool. thanx!

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

21 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

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

NullReferenceException: Object reference not set to an instance of an object PlayerController.Update () (at Assets/Scripts/PlayerController.cs:19) Please Help 2 Answers


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