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 Ful · Oct 01, 2014 at 05:57 PM · javascriptplayerprefstostring

Is it possible to use ToString to format a playerpref int value? (JS)

Hello everyone,

I am sorry for asking the same question again, but not too long ago where I asked if it was possible to re-format and then display a playerpref saved time. I did received an answer and at first I did thought that it was a solution to my question, however now that began to re-work into this particular code section, I find myself lost again.

I just want to know if there was any way I could re-arrange my best time score in a more readable format, for example:

right now when the player survives more than a minute it displays 67 seconds, I would like to know how I could change this to display as a 00:01:07 format.


What I am using right now to display the saved best time:

 var HighscoreText            : GUIText;
 
 function Awake() {
 
 HighscoreText.text = "Best Time: " + PlayerPrefs.GetInt ("timerScore");
 
 }


Initially I thought that perhaps I could use the code String.Format in the script to display the best time score.

Timer Code:

 function OnGUI () { 
 
 var minutes : int = time / 60; 
 
 var seconds : int = time % 60;
 
 var fraction : int = (time * 100) % 100;
 
     GetComponent(GUIText).text = timertext;
  
     timertext = String.Format ("{0:00}:{1:00}:{2:00}", minutes, seconds, fraction);
  
 }


However, it did not worked; I even tried substituting the String.Format to a ToString commend, but the same result always appeared. Though using the String.Format does look promising and perhaps theres a way this could solve my problem?

The attempt:

 Function Start () {
 var minutes : int = time / 60;
 
 var seconds : int = time % 60;
 
 var fraction : int = (time * 100) % 100;

 HighscoreText.text = "Best Score: " + PlayerPrefs.GetInt ("timerScore").ToString ("{0:00}:{1:00}:{2:00}", minutes, seconds, fraction);
 
 }


So here lies my question, would it be possible to somehow tweak the ToString section in the code to make the timer best score display in a 00:00:00 format? If no, then if anyone has a solution please let me know. Additionally if any further information is required, please let me know also thanks in advance!

Complete functional code:

 // Timer
 
 var time                    : float;
 var timer                     : int;
 var timertext                 : String;
 var timeActive              : boolean;
 
 // HighScore
 
 var HighscoreText            : GUIText;
 var playername                 : String; // optional feature, still work in progress
 
 function Awake() {
 
 // Final Playerpref save time code
 
 HighscoreText.text = "Best Time: " + PlayerPrefs.GetInt ("timerScore"); // at the start of the game the highscore will be displayed
 
 }
 
 function Start () {
 
 HighscoreText.guiText.fontSize = 22;
 
 }
 
 function Update () {
 
     if (timeActive) {
     
         timer   = Time.timeSinceLevelLoad;
         time   += Time.deltaTime;
     }
     
     //HighscoreText.guiText.fontSize = 18;
     
      HighscoreUpdate ();
 }
 
 
 function OnGUI () { // though ongui shouldn't be used for mobile, a simple task like this one is fine
 
 //The gui-Time is the difference between the actual time and the start time.
 var minutes : int = time / 60; //Divide the guiTime by sixty to get the minutes.
 
 var seconds : int = time % 60;//Use the euclidean division for the seconds.
 
 var fraction : int = (time * 100) % 100;
 
     GetComponent(GUIText).text = timertext;
  
     timertext = String.Format ("{0:00}:{1:00}:{2:00}", minutes, seconds, fraction);
  
 }
 
 
 function StopTimer2 () {
     
     timer = 0;
     timeActive = false;
 
 }
 
 function HighscoreUpdate () {
 
     // If the value of timer happens to be more than what the current scroe is...
      if ( timer > PlayerPrefs.GetInt ( "timerScore" )) {
      
     // then update the new score, timerScore is a playerpref created which allows the player to save his highscore
             
             PlayerPrefs.SetInt ("timerScore", timer); // timer is the object that contains the value
             PlayerPrefs.Save();
         }
 }
 


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

0 Replies

· Add your reply
  • Sort: 

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

27 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 avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Setting Scroll View Width GUILayout 1 Answer

Can someone help me fix my Javascript for Flickering Light? 6 Answers

HighScore Manage Using PlayerPrefs 1 Answer

Resticing variables to whole numbers 3 Answers

Scripting Tut. 1 Answer


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