- Home /
PlayerPrefsX IntArray
I keep getting the error of index out of range exception for PlayerPrefsX Int Array. I want to replace the highscore whenever score is higher. Here are my codes:
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class ScoreManager : MonoBehaviour
{
public static int score;
public static int[] highScores = new int[2];
public Text scoreText;
public Text highScoreText;
void Awake ()
{
score = 0;
if (PlayerPrefs.HasKey ("HighScore"))
highScores = PlayerPrefsX.GetIntArray ("HighScore");
PlayerPrefsX.SetIntArray ("HighScore", highScores);
}
void Update ()
{
if (Time.timeScale == 0)
{
scoreText.text = "Score: " + score;
highScoreText.text = "High Score: " + highScores [Application.loadedLevel - 1];
if (highScores [Application.loadedLevel - 1] > PlayerPrefsX.GetIntArray ("HighScore") [Application.loadedLevel - 1])
PlayerPrefsX.SetIntArray ("HighScore", highScores);
}
}
}
A function from another class:
void Score ()
{
if (count > ScoreManager.highScores [Application.loadedLevel - 1])
ScoreManager.highScores [Application.loadedLevel - 1] = count;
ScoreManager.score = count;
}
Answer by sumeeton · Apr 04, 2015 at 04:31 AM
Do not use PlayerPrefsX. I have used it and it didn't work as expected when using multiple arrays. Instead build your own with PlayerPrefs. Here is what I have used in one of my projects. I have modified it for your purpose.
// Use this to set integer array
public static void SetHighScores (int[] highScores)
{
PlayerPrefs.SetString ("HighScore", GetSerializedString(highScores));
}
// Use this to get integer array
public static int[] GetHighScores ()
{
string[] data = PlayerPrefs.GetString ("HighScore", "0").Split ('|');
int[] val = new int[data.Length];
int score;
for (int i = 0; i < val.Length; i++)
{
val[i] = int.TryParse(val[i], out score) ? score : 0;
}
return val;
}
private static string GetSerializedString (int[] data)
{
if (data.Length == 0) return string.Empty;
string result = data [0].ToString ();
for (int i = 1; i < data.Length; i++)
{
result += ("|" + data[i]);
}
return result;
}
You may use stringbuilder in GetSerializedString for optimal performance.
is that a typo on line 15? cuz i get an error for that.
Yes, sorry! I changed it on the fly.
Change that line to:
val[i] = int.TryParse(data[i], out score) ? score : 0;
do i need to like call the function sethighscores, gethighscores, and getserializedstring in update or what?
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class Score$$anonymous$$anager : $$anonymous$$onoBehaviour
{
public static int score;
public static int[] highScores = new int[2];
public Text scoreText;
public Text highScoreText;
void Awake ()
{
score = 0;
if (PlayerPrefs.Has$$anonymous$$ey ("HighScore"))
highScores = GetHighScores ("HighScore"); // changed
SetHighScores (highScores); // changed
}
void Update ()
{
if (Time.timeScale == 0)
{
scoreText.text = "Score: " + score;
highScoreText.text = "High Score: " + highScores [Application.loadedLevel - 1];
if (highScores [Application.loadedLevel - 1] > GetHighScores [Application.loadedLevel - 1]) // changed
SetHighScores (highScores); // changed
}
}
// Use this to set integer array
public static void SetHighScores (int[] highScores)
{
PlayerPrefs.SetString ("HighScore", GetSerializedString(highScores));
}
// Use this to get integer array
public static int[] GetHighScores ()
{
string[] data = PlayerPrefs.GetString ("HighScore", "0").Split ('|');
int[] val = new int[data.Length];
int score;
for (int i = 0; i < val.Length; i++)
{
val[i] = int.TryParse(val[i], out score) ? score : 0;
}
return val;
}
private static string GetSerializedString (int[] data)
{
if (data.Length == 0) return string.Empty;
string result = data [0].ToString ();
for (int i = 1; i < data.Length; i++)
{
result += ("|" + data[i]);
}
return result;
}
}