Hello guys, I need some help with the playerprefs used in unity.
My game was done so I reseted the playerprefs. Now that I reseted it with PlayerPrefs.DeleteAll(); It wont save again. I didin’t changed anything to the save mechanic so I dont think I need to upload the scipts? I’m really upset because my game is entirely done, except for the save system
Note that it was working before the reset.
short: PlayerPrefs not saving anymore after reset.
IT ALSO DOESNT REMEMBER THE RESOLUTION AND QUALITY LEVEL ANYMORE.
Thanks in advance.
*Edit, It is saying Debug.Log(“saved”) and “Loaded” when it was needed, but the scene won’t load.
SaveScipt:
using UnityEngine;
using System.Collections;
public class SaveSystem : MonoBehaviour {
// Use this for initialization
void Start () {
PlayerPrefs.SetInt ("TestTheSave", Application.loadedLevel);
PlayerPrefs.Save();
Debug.Log ("saved");
}
}
LoadScript:
using UnityEngine;
using System.Collections;
public class LoadSystem : MonoBehaviour {
public GameObject loadingImage;
void Start () {
Invoke ("LoadTheDamnGame",0);
}
void LoadTheDamnGame(){
if(PlayerPrefs.HasKey("TestTheSave"))
{
loadingImage.SetActive (true);
Application.LoadLevel(PlayerPrefs.GetInt("TestTheSave"));
Debug.Log("Loaded");
}
}
}
Well, it’s still not clear on which objects in which scenes those scripts are attached to. However the scripts seems a bit strange to me. Your “SaveSystem” script saves the current loaded level index immediately when the scene where this script is attached to is started. Likewise your “LoadSystem” immediately reads the stored level index and loads the level. Things like “loading the last level” are usually executed by some kind of user interface (GUI button or similar) and not immediately when the scene starts.
If you have those two scripts attached to gameobjects in the same scene it could easily result in a race condition. For example when the save script is executed before the load script, the load script would always just reads the index of the current level.
Apart from that in your question you said that it doesn’t even save the resolution and quality settings. Are you sure you don’t call RemoveAll somewhere in any of your scripts?
Also when you add Debug.Logs, let them print some useful information. For example:
Debug.Log("going do save level index: " + Application.loadedLevel);
PlayerPrefs.SetInt ("TestTheSave", Application.loadedLevel);
PlayerPrefs.Save();
Debug.Log ("saved. TestTheSave now equals " + PlayerPrefs.GetInt("TestTheSave"));
And in the load script:
if(PlayerPrefs.HasKey("TestTheSave"))
{
loadingImage.SetActive (true);
Debug.Log("going to load level: " + PlayerPrefs.GetInt("TestTheSave"));
Application.LoadLevel(PlayerPrefs.GetInt("TestTheSave"));
Debug.Log("Loaded");
}
else
Debug.Log("TestTheSave doesn't exist in playerprefs");