hi guys!
The problem is the scores dont save after the player gets a high score on the game located on a different scene. The GUI below wont load the high scores. What should I do?
here is the code that im using
using UnityEngine;
using System.Collections;
public class HighScoreC : MonoBehaviour {
// Use this for initialization
void AddScore( string name , int score ){
int newScore;
string newName;
int oldScore;
string oldName;
newScore = score;
newName = name;
for(int i=0;i<10;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 = "";
}
}
}
}
And Script 2
using UnityEngine;
using System.Collections;
public class HighScoreCtwo : MonoBehaviour {
// Use this for initialization
int firstScore;
string firstName;
int secondScore;
string secondName;
int thirdScore;
string thirdName;
int fourthScore;
string fourthName;
string fifthName;
int fifthScore;
void UpdateHScore (){
firstScore = PlayerPrefs.GetInt("0HScore");
firstName = PlayerPrefs.GetString("0HScoreName");
secondScore = PlayerPrefs.GetInt("0Hcore");
secondName = PlayerPrefs.GetString("0HScoreName");
thirdScore = PlayerPrefs.GetInt("0HScore");
thirdName = PlayerPrefs.GetString("0HScoreName");
fourthScore = PlayerPrefs.GetInt("0HScore");
fourthName = PlayerPrefs.GetString("0HScoreName");
fifthScore = PlayerPrefs.GetInt("0HScore");
fifthName = PlayerPrefs.GetString("0HScoreName");
}
void OnGUI (){
GUI.color = Color.black;
GUI.Box( new Rect(100,100,Screen.width,Screen.height), "1) Name Score:");
GUI.color = Color.blue;
GUI.Box( new Rect(100,150,Screen.width,Screen.height),"2) Name Score");
GUI.color = Color.red;
GUI.Box( new Rect(100,200,Screen.width,Screen.height),"3) Name Score");
GUI.color = Color.yellow;
GUI.Box( new Rect(100,250,Screen.width,Screen.height),"4) Name Score");
GUI.color = Color.green;
GUI.Box( new Rect(100,300,Screen.width,Screen.height),"5 Name Score");
}
}
