For Loops Situation
Hi guys
I am trying to retrieve each high score for each scene, but with the following code I seem to be getting the last scene's score from 1 to 925 and not the score of the other scenes. Any ideas what I'm doing wrong? Here is the code: using UnityEngine;
public class CompletedLevels : MonoBehaviour
{
void Start()
{
int isReached = PlayerPrefs.GetInt("Level Completed");
for (int i = 1; i <= isReached; i++)
Debug.Log(i);
float isScored = PlayerPrefs.GetFloat("HighScore");
for (float j = 0; j < isScored; j++)
Debug.Log(j);
}
}
Thanks for any help!
Answer by A_Li_N · Dec 30, 2015 at 05:06 AM
I haven't done anything with PlayerPrefs, but it appears to be for ... player preferences, like keybindings or resolution settings. Not sure it's meant to be used for save data like your code is implying?
Beyond that, without knowing how you are actually storing the information into PlayerPrefs, we can't really tell you why it's not retrieving it.
My quick guesses: When each level is completed, store the level using: PlayerPrefs.SetInt("Level Completed"); and the high score using PlayerPrefs.SetFloat("HighScore");
The problem with this concept is that each level that gets completed will overwrite the previous values:
Complete level 1 with high score of 15, 'Level Completed' is '1' and 'HighScore' is '15'
Complete level 2 with high score of 30, 'Level Completed' is changed to '2' and 'HighScore' is changed to '30'.
'GetInt' and 'GetFloat' will return a single float, not an array.
$$anonymous$$any thanks for your answer. I understand what's going on now. I assume serialization is the way to go?
That's 'a' way to go, but I'm sure there are many others as well :)
Your answer
![](https://koobas.hobune.stream/wayback/20220612054250im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Adding score in if statement within Update() happens continuously. 2 Answers
Level Unlock System....frustrated,Building a level unlock system using player prefs 0 Answers
Score increasing 1 Answer
how to update score and health throughout different scenes. 1 Answer
How do i make Unity seamlessly loop my background music? 5 Answers