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 Matheus_Dem · Oct 16, 2018 at 02:05 AM · playerprefssavestartquithighscore

Saving highscore with PlayerPrefs

So basically I want to save the highscore of the player, so when it closes the game and launch it again the same number (the current highscore) will be displayed. I know that you can do it using PlayerPrefs, but I'm getting some trouble with it. My normal code, without the PlayerPrefs method, is this one (it works fine):

using UnityEngine; using System.Collections; using System.Collections.Generic; using UnityEngine.UI;

public class Highscore Script : MonoBehaviour {

 public static int highscoreValue;
 Text highscore;

 void Start () {
     highscore = GetComponent<Text> ();
 }
 
 void Update () {
     highscore.text = "Highscore: " + highscoreValue;
 }

}

And then I tried to implement the PlayerPrefs, which ended in something like this:

using UnityEngine; using System.Collections; using System.Collections.Generic; using UnityEngine.UI;

public class HighscoreScript : MonoBehaviour {

 public static int highscoreValue;
 Text highscore;

 void Start () {
     highscore = GetComponent<Text> ();
     highscoreValue = PlayerPrefs.GetInt("Highscore");
 }

 void Update () {
     highscore.text = "Highscore: " + highscoreValue;
     PlayerPrefs.SetInt("Highscore", highscoreValue);
     PlayerPrefs.Save();
 }

}

The problem is that, with this new code, the highscore is always 0. And with that I also can't really know if it is saving the highscore or not.

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 Nielssss · Oct 16, 2018 at 02:31 AM 0
Share

I'm not entirely sure so I won't post it as answer, but it might happen because you are doing this in Update. It would overwrite your highscore all the time.

You could try creating a seperate method that saves the highscore, which you only call when there is a highscore.

Something like:

 public void SaveHighscore() {
     PlayerPrefs.SetInt("Highscore", highscoreValue);
     PlayerPrefs.Save();
 }

Not sure when you decide whether the player got a highscore, but I made it public so you can access it in other scripts.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Trevdevs · Oct 16, 2018 at 03:18 AM

Following up with what the person said in the comment definently don't do this in the update function that is horribly inefficent although should work.

If you want to save the score when the player quits the game use this https://docs.unity3d.com/ScriptReference/MonoBehaviour.OnApplicationQuit.html

Comment
Add comment · 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

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

100 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

Related Questions

How can i get the following script back on track to being a single level high score saver 0 Answers

Highscore won't save 3 Answers

PlayerPrefs save after quit? 2 Answers

How to save a high score 2 Answers

Keeping Highest score - Star Rating System 3 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