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?

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)

@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