Save scores on leaderboard using PlayerPrefs

Hi all, I’m posting this as i can’t seem to understand how to use the PlayerPrefs, or if there is a better way to save a few bits of data.

Basically i made a game where you get your score at the end, next to your name (which was typed in earlier). The leaderboard works fine, but once i navigate away from the webpage (or exit play mode in unity engine) it forgets all the high scores.

I am currently displaying the scores and names in guiText, so i want a way for them to save each time a new one is added.

I appreciate any help, many thanks in advance,

Unityyy

p.s i had a look through other posts before creating this one but couldn’t seem to tailor any of the other scripts shown to my needs!

Can you post the code you are using to save and retrieve the scores?

I’ve managed to get the scores and names saving locally now to my machine, but i want them to be stored globally, this is my code, quite a long way to do it inough but i’m not quite comfortable with arrays yet!!

static var score1: int;
static var score2: int;
static var score3: int;
static var score4: int;
static var score5: int;
var scoretemp: int;
static var name1: String;
static var name2: String;
static var name3: String;
static var name4: String;
static var name5: String;
var nametemp: String;

function saveScores()
{
PlayerPrefs.SetInt(“HiScore1”,score1);
PlayerPrefs.SetInt(“HiScore2”,score2);
PlayerPrefs.SetInt(“HiScore3”,score3);
PlayerPrefs.SetInt(“HiScore4”,score4);
PlayerPrefs.SetInt(“HiScore5”,score5);
}

function saveNames()
{
PlayerPrefs.SetString(“HiName1”,name1);
PlayerPrefs.SetString(“HiName2”,name2);
PlayerPrefs.SetString(“HiName3”,name3);
PlayerPrefs.SetString(“HiName4”,name4);
PlayerPrefs.SetString(“HiName5”,name5);
}

function Start () {
getScores();
getNames();
scoretemp = (NewBehaviourScript4.Total);
nametemp = (NewBehaviourScript13.stringToEdit);
if (name1 == nametemp score1 == scoretemp) {
scoretemp = 0;
}
else if (nametemp == name2 scoretemp == score2) {
scoretemp = 0;
}
else if (nametemp == name3 scoretemp == score3) {
scoretemp = 0;
}
else if (nametemp == name4 scoretemp == score4) {
scoretemp = 0;
}
else if (nametemp == name5 scoretemp == score5) {
scoretemp = 0;
}
else if (scoretemp > score1) {
score5 = score4;
score4 = score3;
score3 = score2;
score2 = score1;
score1 = scoretemp;
name5 = name4;
name4 = name3;
name3 = name2;
name2 = name1;
name1 = nametemp;
}
else if (scoretemp > score2) {
score5 = score4;
score4 = score3;
score3 = score2;
score2 = scoretemp;
name5 = name4;
name4 = name3;
name3 = name2;
name2 = nametemp;
}
else if (scoretemp > score3) {
score5 = score4;
score4 = score3;
score3 = scoretemp;
name5 = name4;
name4 = name3;
name3 = nametemp;
}
else if (scoretemp > score4) {
score5 = score4;
score4 = scoretemp;
name5 = name4;
name4 = nametemp;
}
else if (scoretemp > score5) {
score5 = scoretemp;
name5 = nametemp;
}
saveScores();
saveNames();
guiText.text = (score1).ToString();

}

function getScores()
{

if (PlayerPrefs.HasKey(“HiScore1”) == true)
{
score1 = PlayerPrefs.GetInt(“HiScore1”);
score2 = PlayerPrefs.GetInt(“HiScore2”);
score3 = PlayerPrefs.GetInt(“HiScore3”);
score4 = PlayerPrefs.GetInt(“HiScore4”);
score5 = PlayerPrefs.GetInt(“HiScore5”);
}
}

function getNames()
{

if (PlayerPrefs.HasKey(“HiName1”) == true)
{
name1 = PlayerPrefs.GetString(“HiName1”);
name2 = PlayerPrefs.GetString(“HiName2”);
name3 = PlayerPrefs.GetString(“HiName3”);
name4 = PlayerPrefs.GetString(“HiName4”);
name5 = PlayerPrefs.GetString(“HiName5”);
}
}

I was wondering if there was a way to be able to get a certain score on the game, then for someone on a different computer to be able to see it, preferably without using PHP, MySQL or anything like that, so something built into unity.

There isn’t a built-in way to handle highscore servers, but there is an example of how to do it on the Unify wiki. (Unfortunately, it does use PHP/MySQL, etc…)