- Home /
Player Prefs and String Concatenation
Hi,
I'm trying to save the best level times for a mobile game I'm making. There's a level select screen and underneath each level button is the best time that level has been completed in. At the moment this is all taken care of with arrays, etc.
But getting PlayerPrefs to save these times is where I'm having trouble. At the moment I have this part of a function that runs every time a level is completed:
if (levelTimes[levelNumber] < time){ // set best time
levelTimes[levelNumber] = time;
PlayerPrefs.SetFloat("Level Time: " + levelTimes[levelNumber].ToString(), time);
}
And then when the game starts up, I try to get that time by calling this:
noOfLevelsCompleted = PlayerPrefs.GetInt ("LevelsCompleted");
for (int i = 0; i <= noOfLevelsCompleted; i++){
levelsCompleted[i] = true;
levelsAvailable[i] = true;
if (i !=9){
levelsAvailable[i + 1] = true;
}
levelTimes[i] = PlayerPrefs.GetFloat("Level Time: " + levelTimes[i].ToString());
}
Any idea why this isn't working? Will I have to create a separate PlayerPrefs for each level and save the best time like that? I could probably figure that out, but it seems like a bit of a roundabout way of doing it to me.
Answer by gjf · Jul 17, 2014 at 02:25 PM
shouldn't
PlayerPrefs.SetFloat("Level Time: " + levelTimes[levelNumber].ToString(), time);
just be something like (no need for spaces, etc.)
PlayerPrefs.SetFloat("LevelTime" + levelNumber, time);
also
for (int i = 0; i <= noOfLevelsCompleted; i++){
should probably be
for (int i = 0; i < noOfLevelsCompleted; i++){
Ah, thanks, gjf. That worked perfectly. I guess I was just overthinking it.
Your answer
