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 /
  • Help Room /
avatar image
0
Question by $$anonymous$$ · Oct 05, 2016 at 02:36 PM · c#playerprefshighscorestimers

How do I save my Highest time using playerprefs?

Hi,

I'm currently working on a endless runner game and im struggling with my high score. I have a timer script that works just fine, but I want to able to save the highest score using playerprefs. Does anyone have a clue on how I can do this?

Here's my current timer script:

 using UnityEngine;
 using System.Collections;
 using UnityEngine.UI;
 
 public class Timer : MonoBehaviour {
 
      public Text timer;
     public float highscore = 0;
     public float seconds = 00;
     public float miliseconds = 00;
     static public float minutes = 00;
     public Canvas canvas;
 
     void FixedUpdate(){
         if (miliseconds >= 60) {
             seconds++;
             miliseconds = 00;
         }    
 
         if (seconds >=60) {
             minutes++;
             seconds = 00;
         }   
 
         if (GameOver.gameover == false) {
             miliseconds += Time.deltaTime * 60;
         }
         timer.text = minutes.ToString ("00") + ":" + seconds.ToString ("00") + ":" + miliseconds.ToString ("00");
     }
 }
Comment
Add comment · Show 1
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 Bonfire-Boy · Oct 05, 2016 at 03:11 PM 0
Share

Try searching for the term "PlayerPrefs" using the search box above, there are plenty of examples already in these pages.

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by JScotty · Oct 05, 2016 at 03:15 PM

Hi Joey,

You can save your highscore time just by doing :

 PlayerPrefs.SetInt("Minutes", minutes);
 PlayerPrefs.SetInt("Seconds", seconds);
 PlayerPrefs.SetInt("Miliseconds", miliseconds);

after you've done that, and you want to check if your new score is bigger than your saved score do:

 int highscoreMins = PlayerPrefs.GetInt("Minutes"); 
 int highscoreSec = PlayerPrefs.GetInt("Seconds"); 
 int highscoreMilisec = PlayerPrefs.GetInt("Miliseconds"); 
 
 //compare those with your old ones
 
 if(minutes > highscoreMins){
    //new highscore
   SetNewHighscore();
 } else if(minutes == highscoreMins){
    if(seconds > highscoreMins){
       //new highscore
      SetNewHighscore();
    }
    if(seconds == highscoreSec){
        if(miliseconds > highscoreMilisec){
       //new highscore
      SetNewHighscore();
       } else {
       //do nothing
       }
    }
 }


 void SetNewHighscore(){
    PlayerPrefs.SetInt("Minutes", minutes);
    PlayerPrefs.SetInt("Seconds", seconds);
    PlayerPrefs.SetInt("Miliseconds", miliseconds); 
 }

hope it helped a bit more info: https://docs.unity3d.com/ScriptReference/PlayerPrefs.html

Comment
Add comment · Show 6 · 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 JScotty · Oct 05, 2016 at 03:18 PM 0
Share

I also recommend to use const strings to declare your saveData names like:

  const string $$anonymous$$utesPlayerPrefs = "$$anonymous$$inutes"; 
  const string secondsPlayerPrefs = "Seconds";
  const string milisecondsPlayerPrefs = "$$anonymous$$iliseconds";

and then when you try to get or set your prefs use:

  PlayerPrefs.SetInt($$anonymous$$utesPlayerPrefs , $$anonymous$$utes); // set $$anonymous$$utes 
  int highscore$$anonymous$$inutes = PlayerPrefs.Getint($$anonymous$$utesPlayerPrefs); // get $$anonymous$$utes
      
  PlayerPrefs.SetInt(secondsPlayerPrefs , seconds); // set seconds 
  int highscoreSeconds = PlayerPrefs.Getint(secondsPlayerPrefs); // get seconds
      
  PlayerPrefs.SetInt(milisecondsPlayerPrefs , miliseconds); // set miliseconds 
  int highscore$$anonymous$$iliseconds = PlayerPrefs.Getint(milisecondsPlayerPrefs); // get miliseconds
 
 to prevent misspelling, and errors. 


avatar image Bonfire-Boy · Oct 05, 2016 at 03:26 PM 0
Share

You need to save the player prefs too.

avatar image JScotty · Oct 05, 2016 at 03:43 PM 0
Share

You save a playerpref by doing PlayerPrefs.SetInt("$$anonymous$$inutes", $$anonymous$$utes);

avatar image Bonfire-Boy JScotty · Oct 05, 2016 at 04:00 PM 0
Share

The changes you make using the Set functions will not persist unless you subsequently call PlayerPrefs.Save()

avatar image Owen-Reynolds · Oct 05, 2016 at 04:05 PM 0
Share

It's more common to convert to a single unit, like only seconds. $$anonymous$$akes the code faster and less error-prone. For example, there's a small bug where you compare seconds to $$anonymous$$utes. Not completely on you, since the OP is already using the not-so-good seconds+$$anonymous$$utes way.

The conversion to $$anonymous$$utes/seconds/etc is generally then done only when displaying to the human.

avatar image $$anonymous$$ · Oct 06, 2016 at 07:30 AM 0
Share

Thx man it worked!

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Highscore Not Updating Upon Death 1 Answer

how do i store highscore locally C# (SIMPLE) 3 Answers

Highscore and PlayerPrefs unity C# 1 Answer

Persistent data between different games - local save for WebGL 0 Answers

Use PlayerPrefs to hard reset game without setting all values to 0 0 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