- Home /
HighScore Manage Using PlayerPrefs
I want to manage HighScore levelwise..For that I am using PlayerPrefs plugin.
I have written below code to manage highscore:
ScoreController Script in JS:
public var highScoreList : List.<int> = new List.<int>();
function Start () {
LoadData();
gameControllerScript = GameObject.Find("Main Camera").GetComponent("GameController");
}
function LoadData()
{
for (var i : int = 0; i < highScoreList.Count; i++)
{
highScoreList[i] = PreviewLabs.PlayerPrefs.GetInt("highScore" + i);
}
}
function Update () {
if (!once)
{
if(GameController.Score >= gameControllerScript.objectiveScore)
{
if(GameController.HighScore < GameController.Score)
{
highScoreList[GameController.level] = GameController.Score; // Error Comes From Here
Debug.Log("new high score: " + GameController.Score);
SaveData();
}
once = true;
}
}
}
function SaveData()
{
for (var i : int = 0; i < highScoreList.Count; i++)
{
PreviewLabs.PlayerPrefs.SetInt("highScore" + i,highScoreList[i]);
}
PreviewLabs.PlayerPrefs.Flush();
}
But it gives me ArgumentOutOf range Exception.. So how do i store and update my highscore for perticuler level?
Consol :
Argument is out of range.
Parameter name: index
System.Collections.Generic.List`1[System.Int32].CheckIndex (Int32 index)
System.Collections.Generic.List`1[System.Int32].set_Item (Int32 index, Int32 value)
Pleaze Help me..
Thanx for your Support and Help..
Please copy the error message from your Console and post it here. Also add a comment to the code above on the line that is generating the IndexOutOfRange Exception. Note you have not included the code that initializes 'higheScoreList' above.
@Eric5h5 But What is the problem with this script?? and sir can you plese show me the way of using ArrayPrefs2??
The page I linked to has instructions; read them. I posted that as a comment ins$$anonymous$$d of an answer because if you use ArrayPrefs2, you don't need that script at all.
Answer by robertbu · Apr 12, 2013 at 06:04 AM
You are using a list, but you are not adding any elements into the list. So when you index into the list you get an error. If you know ahead of time the number of levels you will have you can use a built-in array with very little restructuring of your code.
var highScoreList: int[] = new int[100];
Then you will use 'Length' instead of 'Count':
for (var i : int = 0; i < highScoreList.Length; i++)
Now it throws error At Same Point:
IndexOutOfRangeException: Array index is out of range.
ScoreController.Update () (at Assets/Scripts/ScoreController.js:62)
You are saying that GameController.level == 1? You Debug.Log()ed this to verify? And did you initialize highScoreList?
yes sir.. and yes gamecontroller.level == 1.. i have debugged this to verify..
Your answer
Follow this Question
Related Questions
How would I transfer these into PlayerPrefs? 0 Answers
how to save and retreive high score.... 4 Answers
displaying playerprefs 1 Answer
Highscoring and changing scripts (Java/UnityScript) 1 Answer