- Home /
PlayerPrefs performing function even when it doesn't have that key
When I open my scene, I want to choose whether to display one GameObject, or another based on where I've just navigated from. On some buttons I have the code:
public void Xmas(){
MapKey = "OpenScene";
PlayerPrefs.SetInt("OpenScene", 1);
PlayerPrefs.Save();
SceneManager.LoadScene("XmasStage");
Debug.Log("KEY IS " + MapKey);
}
On other buttons I have:
public void BackToMap(){
MapKey = "OpenMap";
PlayerPrefs.SetInt("OpenMap", 1);
PlayerPrefs.Save();
SceneManager.LoadScene("XmasStage");
Debug.Log("KEY IS " + MapKey);
}
On the Awake function of the "XmasStage" scene, I check to see which key has saved and turn on the right GameObject accordingly:
void Awake()
{
if(PlayerPrefs.HasKey("OpenMap")){
map.SetActive(true);
XmasMenu.SetActive(false);
Debug.Log("OPENING MAP");
}
if(PlayerPrefs.HasKey("OpenScene")){
map.SetActive(false);
XmasMenu.SetActive(true);
Debug.Log("OPENING MENU");
}
Debug.Log("KEY IS " + MapKey);
}
As you can see, I've got a lot of Debug.Logs to try and keep track of what's going on.
When I press the button that sets the key, the debug log shows the correct key.
When it loads the scene, it still shows the correct key but then performs both PlayerPrefs functions; in the debug log it says "OPENING MAP" then "OPENING MENU" meaning it will always end up activating the OPENING MENU GameObject
Can anyone tell me what I'm doing wrong?
Answer by rh_galaxy · Nov 17, 2020 at 03:43 PM
It will have both keys if you have run PlayerPrefs.SetInt and Save at any time before... The keys are never deleted unless you do it yourself. Perhaps what you need is another key (i.e prefs variable)
PlayerPrefs.SetInt("LastOpen", 0); //meaning menu
PlayerPrefs.SetInt("LastOpen", 1); //meaning map
PlayerPrefs.GetInt("LastOpen", 0); //will give 0 (menu) if the key does not exist (only happens first time)
Thank you! I assumed that it would overwrite rather than just save both. Your method works, as does deleting the key just before you set it, so it only saves one key.
PlayerPrefs.DeleteKey($$anonymous$$apKey);
Answer by hollym16 · Nov 19, 2020 at 12:01 PM
@rh_galaxy I'm trying to implement your method but I'm a bit unclear on how it works. I understand that the GetInt will get the number based on what you SetInt, but how do you then declare what to do, based on that number?
In my previous script I used:
if(PlayerPrefs.HasKey("OpenScene")){
//Do something
Do I need to do something similar with GetInt, such as:
if(PlayerPrefs.GetInt == 2){
//Do something
Everything you do with PlayerPrefs involves a Key, that's a name that you associate with the Data. Data can be a string or an int or a float or other types.
SetInt("LastOpen", 1) sets the Key to LastOpen and the data to an int that has value 1. GetInt("LastOpen", 0) gets the value 1 from the Key LastOpen, but if the key does not exist it returns the value 0.
PlayerPrefs.HasKey("LastOpen") will return true if the Key exists, regardless of the value, it can be 100 or -65 or 1. What you decide to do when a value is 0 or 1 or 5 is up to you in the code. You don't need to use PlayerPrefs.HasKey at all, just use GetInt or SetInt in your case.
Thanks for the reply. So would I use something like this? key = "LastOpen";
PlayerPrefs.GetInt("LastOpen", 0); if(key== 0){ //Do something
This is not so hard, from your original post, you would do:
public void $$anonymous$$(){
PlayerPrefs.SetInt("LastOpen", 1);
PlayerPrefs.Save();
Scene$$anonymous$$anager.LoadScene("$$anonymous$$Stage");
}
public void $$anonymous$$enu(){
PlayerPrefs.SetInt("LastOpen", 0);
PlayerPrefs.Save();
Scene$$anonymous$$anager.LoadScene("$$anonymous$$enu");
}
int value = PlayerPrefs.GetInt("LastOpen", 0);
if(value==1) {
Debug.Log("OPENING X$$anonymous$$AS $$anonymous$$AP");
}
if(value==0) {
Debug.Log("OPENING $$anonymous$$ENU");
}