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
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);
}
thank you that is exactly what i was looking for. it works too.
thanks again dewd :)
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;
}
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;
}
}
}
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 ();
}
}
@georgiatolotti: your call should be StoreHighscore(score)
, and not StoreHighscore(newHighscore)
Your answer
Follow this Question
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