Problem saving with PlayerPrefs
Hey!
So, what I have is a PlayerPrefs.SetInt at the end of my level one (as well in a shortcut in this same level) wich sets a Static Int to 2 (to load the level 2). It's working fine and everything. But if someone doesn't ends the firts level and quits (quits by the main menu), the level saved in the Int is the 2nd wich is a nonsense since (I think) i have set it property to avoid this kind of issue.
I post the code for my MainMenu so you can try to tell me.
public class MainMenuButtons : MonoBehaviour {
[Header ("UI")]
public GameObject uiMainMenu;
public GameObject uiOptions;
public GameObject uiExit;
public GameObject loadingScreen;
[Header ("Audio")]
public AudioSource Wind;
public GameObject SFXFireCamp1;
[Header ("Others")]
public Slider sliderBar;
public Text progressText;
const int LEVEL_01 = 1;
const int LEVEL_02 = 2;
const int LEVEL_03 = 3;
public static int levelToOpen = 1;
void Awake () {
}
void Start () {
levelToOpen = PlayerPrefs.GetInt ("LevelToLoad");
print (PlayerPrefs.GetInt ("LevelToLoad"));
if (levelToOpen == LEVEL_01) {
Debug.LogWarning ("S'obrira el Level_01!!");
}
if (levelToOpen == LEVEL_02) {
Debug.LogWarning ("S'obrira el Level_02!!");
}
if (levelToOpen == LEVEL_03) {
Debug.LogWarning ("S'obrira el Level_03!!");
}
Time.timeScale = 1;
Cursor.lockState = CursorLockMode.Confined;
SFXFireCamp1 = GameObject.Find ("PS_CartoonFlames01");
uiMainMenu.SetActive (true);
uiOptions.SetActive (false);
uiExit.SetActive (false);
}
void Update () {
Time.timeScale = 1;
ExitPopUp ();
if (Input.GetKeyDown (KeyCode.R)) {
Debug.LogWarning ("Resetejat, s'obrira el Level_01");
levelToOpen = LEVEL_01;
}
}
public void ButtonPlay (int sceneIndex) {
if (levelToOpen == LEVEL_01) {
StartCoroutine (LoadAsyncProgress (sceneIndex));
}
if (levelToOpen == LEVEL_02) {
StartCoroutine (LoadAsyncProgress2 (sceneIndex));
}
if (levelToOpen == LEVEL_03) {
StartCoroutine (LoadAsyncProgress3 (sceneIndex));
}
}
IEnumerator LoadAsyncProgress (int sceneIndex) {
AsyncOperation operation = SceneManager.LoadSceneAsync ("Level_01");
loadingScreen.SetActive (true);
while (!operation.isDone) {
float progress = Mathf.Clamp01 (operation.progress / .9f);
sliderBar.value = progress;
progressText.text = progress * 100f + "%";
yield return null;
}
}
IEnumerator LoadAsyncProgress2 (int sceneIndex) {
AsyncOperation operation = SceneManager.LoadSceneAsync ("Level_02");
loadingScreen.SetActive (true);
while (!operation.isDone) {
float progress = Mathf.Clamp01 (operation.progress / .9f);
sliderBar.value = progress;
progressText.text = progress * 100f + "%";
yield return null;
}
}
IEnumerator LoadAsyncProgress3 (int sceneIndex) {
AsyncOperation operation = SceneManager.LoadSceneAsync ("Level_03");
loadingScreen.SetActive (true);
while (!operation.isDone) {
float progress = Mathf.Clamp01 (operation.progress / .9f);
sliderBar.value = progress;
progressText.text = progress * 100f + "%";
yield return null;
}
}
public void ButtonQuit () {
PlayerPrefs.Save ();
Debug.LogWarning ("Progres Guardat");
Debug.Log ("S'obrira el nivell " + levelToOpen);
print (PlayerPrefs.GetInt ("LevelToLoad"));
Application.Quit ();
}
public void ButtonCredits () {
SceneManager.LoadScene ("Credits");
}
public void ExitPopUp () {
if (uiExit.activeSelf == true) {
Wind.volume = 0.5f;
SFXFireCamp1.GetComponent <AudioSource> ().volume = 0.5f;
if (Input.GetKeyDown (KeyCode.Escape)) {
uiExit.SetActive (false);
uiMainMenu.SetActive (true);
}
} else {
Wind.volume = 1f;
SFXFireCamp1.GetComponent <AudioSource> ().volume = 1f;
}
}
}
Basically everytime I open the main menu it sets the leveltoopen to 2. I can't find out what it's causing it, so some help would be usefull!
Also, the next line wich is in the ButtonQuit void prints 2 but the debug.log says 1. print (PlayerPrefs.GetInt ("LevelToLoad"));
And also 2: Sorry for my english, not an english speaker! :'D
Your answer
Follow this Question
Related Questions
PlayerPrefs help ! 1 Answer
Saving In Between Scenes and Program Exit 0 Answers
How do I apply and save my game settings? 0 Answers
PlayerPrefs Not Saving When I Click the Button. 1 Answer
Using PlayerPrefs to remember transform heirarchies? 0 Answers