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 *= 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*);*
}
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.List1[System.Int32].CheckIndex (Int32 index)*_ <em>*System.Collections.Generic.List1[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.

oops.. sorry sir. its argumentoutofrange..

Use [ArrayPrefs2][1] to save arrays using PlayerPrefs. [1]: http://wiki.unity3d.com/index.php?title=ArrayPrefs2

@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 instead of an answer because if you use ArrayPrefs2, you don't need that script at all.

1 Answer

1

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)

My level variable is having 1 value..

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..

Thanx sir.. Your solution works for me..