Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 lessthanthreewizard · Apr 05, 2020 at 05:22 AM · playerprefstimerhighscoreshighscore

Timer highscore trouble

Hello. I've been trying to have a time based high score in my game and so far I've been able to save my time when finishing a level and displaying it using player.prefs. The problem I'm facing now is that I don't know how to save a high score that would replace the current time that I already have on a level. For example, when a player finishes a level it would compare the new time to the previous time and if it's better it will save the new one if it's worse it will discard it. Here's the script I'm using for my timer

 public class StopWatch : MonoBehaviour
 {
     public float timeStart;
     public Text textBox;
     public float seconds, minutes, milliseconds, hours;
     bool timerActive = true;
 
     // Use this for initialization
     void Start()
     {
         textBox.text = timeStart.ToString("F2");
     }
 
     // Update is called once per frame
     void Update()
     {
         if (timerActive)
         {
             hours = (int)(Time.timeSinceLevelLoad / 3600f);
 
 
             milliseconds = (int)(Time.timeSinceLevelLoad * 1000f) % 1000;
 
             minutes = (int)(Time.timeSinceLevelLoad / 60f) % 60;
 
             seconds = (int)(Time.time % 60f);
 
             textBox.text = hours.ToString("00") + ":" + minutes.ToString("00") + ":" + seconds.ToString("00") + ":" + milliseconds.ToString("00");
 
 
         }
     }



and here is the code I'm using to save the time to player.prefs

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 
 public class SaveTime : MonoBehaviour
 {
     public Text Time;
     string _currenttime;
 
     public void SetTime()
     {   
         _currenttime = Time.text;
         PlayerPrefs.SetString("Time", _currenttime);
         Debug.Log ("Your Time Is " + PlayerPrefs.GetString("Time"));
 
     }
 
 
     }


If anyone could help me or guide me in the right direction I would greatly appreciate it.

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
0

Answer by n_rusev · Apr 05, 2020 at 06:27 AM

Hello. Since you already have your time in seconds with Time.timeSinceLevelLoad, why not just save it like a float and compare later?

You can do:

 float timeInSeconds = Time.timeSinceLevelLoad;
 if(timeInSeconds < PlayerPrefs.GetFloat("Time", 9999999f) )
 {
     PlayerPrefs.SetFloat("Time", timeInSeconds);
 } 

The second parameter to PlayerPrefs.GetFloat is the default value that will be returned, if nothing is saved under "Time". I've made very big number, so that your current time will always be smaller than it. And ofcourse if something is returned and your timeInSeconds is again smaller than it, it will be overwritten.

Comment
Add comment · Show 7 · 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 lessthanthreewizard · Apr 05, 2020 at 07:16 PM 0
Share

Hello! thank you for the reply. That seems to work wonderfully the only question left is how would I go about displaying the timeinseconds float onto a text? I tried converting it into a string but that didn't seem to work

avatar image BBIT-SOLUTIONS lessthanthreewizard · Apr 05, 2020 at 07:34 PM 1
Share

Hello, why does converting not work? How did you do it? What error do you get?

Actually a simple timeInSeconds.ToString() should work.

avatar image lessthanthreewizard BBIT-SOLUTIONS · Apr 05, 2020 at 07:50 PM 0
Share

Hey, I'm doing this to try and convert it and display it onto a textbox

  timeInSeconds.ToString(_timeinstring);
         PlayerPrefs.SetString("TimeInString", _timeinstring);
         _stringtext.text = _timeinstring;
Show more comments

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

133 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 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 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 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 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

How do I make a Highscore for my timer? 1 Answer

"Best Time"/high score help… 1 Answer

How can I save highscores and display them for later? 1 Answer

PlayerPref set int and get int 1 Answer

Can someone help me with my highscore script? 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