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 /
  • Help Room /
avatar image
2
Question by Bill Got Game · Feb 20, 2014 at 03:42 PM · c#playerprefshighscoreshighscore

how do i store highscore locally C# (SIMPLE)

hi guys i know this question has been asked already but i was too much of a noob to implement them.

im looking for help storing a highscore(int) locally on a mobile device.

i only need it to store 1 entry(just highscore ,not a leaderboard). no need for a name entry either,

i just want the INT to be stored. the simplest form of highscore recording...i'd imagine.

i've heard PlayerPrefs is the way to go

i would love some help with this. i have never done anything like this on unity before i would be many thankful

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

3 Replies

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

Answer by DerWoDaSo · Feb 20, 2014 at 04:06 PM

Yes, use the PlayerPrefs. Its really easy to do, just like this:

 void StoreHighscore(int newHighscore)
 {
     int oldHighscore = PlayerPrefs.GetInt("highscore", 0);    
     if(newHighscore > oldHighscore)
         PlayerPrefs.SetInt("highscore", newHighscore);
 }
Comment
Add comment · Show 4 · 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 Bill Got Game · Feb 21, 2014 at 02:38 PM 0
Share

thank you that is exactly what i was looking for. it works too.

thanks again dewd :)

avatar image Dblfstr · Feb 21, 2014 at 03:53 PM 0
Share

Don't forget PlayerPrefs.Save();

avatar image gimoj · Jul 10, 2014 at 07:39 PM 0
Share

If it isn't any trouble, maybe you could post how to display this? I used this code but I can't seem to display it. Here's my code for displaying it:

 public const int BASE_WIDTH = 1024;
 public const int BASE_HEIGHT = 600;
 public int FSize = 50;
 
 private float horizontalRatio;
 private float verticalRatio;
 
 public float ScoreX;
 public float ScoreY;    
 
 // Use this for initialization
 void Start () {
 
 }
 
 // Update is called once per frame
 void Update () {
     
     horizontalRatio = Screen.width / BASE_WIDTH;
     verticalRatio = Screen.height / BASE_HEIGHT;
     
     //        var scoreposx = horizontalRatio / ScoreX;
     //        var scoreposy = verticalRatio / ScoreY;
     
     guiText.text = "Score: " +PlayerPrefs.GetInt("highscore");
     guiText.fontSize = $$anonymous$$athf.$$anonymous$$in(BASE_HEIGHT,BASE_WIDTH)/FSize;
     
 }
avatar image Jana1108 · Jul 07, 2015 at 03:44 AM 0
Share

I saw this but can't get it working... this is my code can someone please help?

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

public class Highscore : $$anonymous$$onoBehaviour {

 public Text scoreText;
 public float score;
 public Text highscoreText;
 
 // Use this for initialization
 void Start () {
     
     scoreText.text = "Score: " + score.ToString();
     
 }
 
 // Update is called once per frame
 void Update () {
     
     highscoreText.text = "Best: " + PlayerPrefs.GetInt("highscore");
     
 }
 
 void StoreHighscore(int newHighscore)
 {
     int oldHighscore = PlayerPrefs.GetInt("highscore", 0);    
     if(newHighscore > oldHighscore)
     {
         PlayerPrefs.SetInt("highscore", newHighscore);
         highscoreText = scoreText;
     }
 }

}

avatar image
0

Answer by Mti_Tareq · Feb 17, 2016 at 03:23 PM

@jana1108..You have to Add PlayerPrefs.Save(); after if condition in StoreHighScore() and should call this method in Update()...it should work fine..

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

Answer by georgiatolotti · Aug 23, 2017 at 08:38 PM

Mine is like this and its not working, any help please? :(

 public class Score : MonoBehaviour {

 public Text txtScore;
 public static int score;

 public Text txtHighscore;
 public static int highscore;
 int newHighscore;

 void Start (){
     score = 0;
 }

 void Update (){
     txtScore.text = "Score: " + score.ToString ();
     txtHighscore.text = "Highscore: " + PlayerPrefs.GetInt ("highscore");
     StoreHighscore (newHighscore);
 }

 void StoreHighscore (int newHighscore)
 {
     int oldHighscore = PlayerPrefs.GetInt ("highscore", 0);
     if (newHighscore > oldHighscore) 
     {
         PlayerPrefs.SetInt ("highscore", newHighscore);
         txtHighscore = txtScore;
     }
     PlayerPrefs.Save ();
 }

}

Comment
Add comment · Show 1 · 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 jlsmorcilla · Sep 11, 2017 at 03:18 PM 0
Share

@georgiatolotti: your call should be StoreHighscore(score), and not StoreHighscore(newHighscore)

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

24 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

Related Questions

Highscore Not Updating Upon Death 1 Answer

Highscore not working 0 Answers

How do I access playerprefs from another script? 1 Answer

A way to move "Highscore" Text to Game Over Screen? 1 Answer

Highscore and PlayerPrefs unity C# 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