- Home /
Question by
Az-Strix · Jun 28, 2015 at 03:49 AM ·
arrayplayerprefshighscores
PlayerPrefs Not Working
Hey Guys, I am making a highscore system. It is more like a leader board. Instead of playing the game to get the highscore, the user inputs their highscore and name. I wanted this to be stored into player prefs like a 2D array, and in the next scene, the user sees the leaderboard. If it is not a highscore, then it doesn't update the list (makes no changes to it). The first script is called Highscore.cs and the other script used in the next scene where the leaderboard lies, is called HighscoresList.cs. It says the build was successful but when played, it is not working. Please help!
Highscore.cs
using UnityEngine;
using System.Collections;
using System;
public class Highscore : MonoBehaviour {
string username = "";
string inputScore = "";
int score;
void OnGUI () {
//Score Input
GUI.Label(new Rect (225, 93, 250, 25), "Score : ");
inputScore = GUI.TextField (new Rect (275, 93, 250, 25), inputScore, 50);
//User Name Input
GUI.Label (new Rect (225, 125, 250, 25), "Name : ");
username = GUI.TextField (new Rect (275, 125, 250, 25), username, 50);
AddScore();
//Submit Button
if (GUI.Button (new Rect (300, 250, 100, 30), "Submit")) {
Debug.Log(username + " " + inputScore);
//Converting string numbers to int and assigning to int score
score = int.Parse(inputScore);
Application.LoadLevel(3);
}
}
public void AddScore(){
int newScore;
string newName;
int oldScore;
string oldName;
newScore = score;
newName = name;
for(int i = 0; i < 5; i++) {
if(PlayerPrefs.HasKey(i + "HScore")) {
if(PlayerPrefs.GetInt(i + "HScore") < newScore) {
//new score is higher than the stored score
oldScore = PlayerPrefs.GetInt(i + "HScore");
oldName = PlayerPrefs.GetString(i + "HScoreName");
PlayerPrefs.SetInt(i + "HScore",newScore);
PlayerPrefs.SetString(i + "HScoreName", newName);
newScore = oldScore;
newName = oldName;
}
} else {
PlayerPrefs.SetInt(i + "HScore", newScore);
PlayerPrefs.SetString(i + "HScoreName", newName);
newScore = 0;
newName = "";
}
}
}
}
HighscoresList.cs
using UnityEngine;
using System.Collections;
public class HighscoresList : MonoBehaviour {
int FirstScore = PlayerPrefs.GetInt("0HScore");
string FirstName = PlayerPrefs.GetString("0HScoreName");
int SecondScore = PlayerPrefs.GetInt("1HScore");
string SecondName = PlayerPrefs.GetString("1HScoreName");
int ThirdScore = PlayerPrefs.GetInt("2HScore");
string ThirdName = PlayerPrefs.GetString("2HScoreName");
int FourthScore = PlayerPrefs.GetInt("3Score");
string FourthName = PlayerPrefs.GetString("3HScoreName");
int FifthScore = PlayerPrefs.GetInt("4HScore");
string FifthName = PlayerPrefs.GetString("4HScoreName");
public TextMesh NameOne;
public TextMesh ScoreOne;
public TextMesh NameTwo;
public TextMesh ScoreTwo;
public TextMesh NameThree;
public TextMesh ScoreThree;
public TextMesh NameFour;
public TextMesh ScoreFour;
public TextMesh NameFive;
public TextMesh ScoreFive;
void ChangeText() {
ScoreOne.text = FirstScore.ToString();
ScoreTwo.text = SecondScore.ToString();
ScoreThree.text = ThirdScore.ToString();
ScoreFour.text = FourthScore.ToString();
ScoreFive.text = FifthScore.ToString();
NameOne.text = FirstName.ToString();
NameTwo.text = SecondName.ToString();
NameThree.text = ThirdName.ToString();
NameFour.text = FourthName.ToString();
NameFive.text = FifthName.ToString();
}
}
Comment