Search Unity

PlayerPreferences aren't saved on Android

Discussion in 'Android' started by SirioRigel, Jul 28, 2021.

  1. SirioRigel

    SirioRigel

    Joined:
    Apr 10, 2021
    Posts:
    27
    So when I try to save PlayerPrefs on android it doesn't work while on the editor it works just fine!
    I can't see the problem here, but here's the code for reference, I have also set some debug logs and in console they are printed correctly but on android these preferneces aren't saved and also when the game launches they are ignored. Thx in advance
    Code (CSharp):
    1.     private void Start()
    2.     {
    3.         particlesLow.image.sprite = defaultButton;
    4.         particlesMed.image.sprite = defaultButton;
    5.         particlesHigh.image.sprite = defaultButton;
    6.         selectedbutton = Convert.ToInt32(PlayerPrefs.GetFloat("selectedbutton"));
    7.         switch (selectedbutton)
    8.         {
    9.             case 1:
    10.                 particlesLow.image.sprite = selectedButton;
    11.                 break;
    12.             case 2:
    13.                 particlesMed.image.sprite = selectedButton;
    14.                 break;
    15.             case 3:
    16.                 particlesHigh.image.sprite = selectedButton;
    17.                 break;
    18.         }
    19. audioOn = Convert.ToBoolean(audioprefs); //assgns the value of the playerprefs of the audio to audioOn
    20.      
    21.         SettingsPanel.SetActive(false);
    22.         if (audioOn)
    23.         {
    24.             AudioListener.volume = 1; //sets the volume to 1 if it's off
    25.             audioButton.image.sprite = AudioOn;
    26.         }
    27.         else
    28.         {
    29.             AudioListener.volume = 0; //sets the volume to zero if it's on
    30.             audioButton.image.sprite = AudioOff;   //we assign the sprite that we had declared
    31.         }
    32.  
    33.     }
    34.  
    35.    private void OnApplicationQuit()
    36.     {
    37.         //PlayerPrefs.SetFloat("AudioValue", audioSlider.value); //saves the audio volume
    38.         PlayerPrefs.SetString("Audio", audioOn.ToString()); //saves the audio prefernces
    39.         PlayerPrefs.SetFloat("selectedbutton", selectedbutton);
    40.         PlayerPrefs.Save();
    41.         Debug.Log(PlayerPrefs.GetString("Audio"));
    42.         Debug.Log(PlayerPrefs.GetFloat("selectedbutton"));
    43.     }
     
    Last edited: Jul 28, 2021
  2. kaarloew

    kaarloew

    Joined:
    Nov 1, 2018
    Posts:
    360
  3. SirioRigel

    SirioRigel

    Joined:
    Apr 10, 2021
    Posts:
    27
  4. kaarloew

    kaarloew

    Joined:
    Nov 1, 2018
    Posts:
    360
    Problem with Android (and iOS) is that there isn't any reliable OnApplicationQuit, so you shouldn't trusting to it.
     
  5. SirioRigel

    SirioRigel

    Joined:
    Apr 10, 2021
    Posts:
    27
    I tried using OnApplicationPause, I check if the device is a mobile and try to call it and save the preferences but still they are not saved; Then I use OnApplicationQuit only for desktop how you seggested, here's the code:
    Code (CSharp):
    1.     private void OnApplicationPause(bool hasFocus)
    2.     {
    3.         if (Application.isMobilePlatform) paused = hasFocus;
    4.     }
    5.     private void PlayerPrefsSave()
    6.     {
    7.         if (paused)
    8.         {
    9.             //PlayerPrefs.SetFloat("AudioValue", audioSlider.value); //saves the audio volume
    10.             PlayerPrefs.SetString("Audio", audioOn.ToString()); //saves the audio prefernces
    11.             PlayerPrefs.SetFloat("selectedbutton", selectedbutton);
    12.             Debug.Log(PlayerPrefs.GetString("Audio"));
    13.             Debug.Log(PlayerPrefs.GetFloat("selectedbutton"));
    14.         }
    15.     }
    16.     private void OnApplicationQuit()
    17.     {
    18.         if (!Application.isMobilePlatform)
    19.         {
    20.             //PlayerPrefs.SetFloat("AudioValue", audioSlider.value); //saves the audio volume
    21.             PlayerPrefs.SetString("Audio", audioOn.ToString()); //saves the audio prefernces
    22.             PlayerPrefs.SetFloat("selectedbutton", selectedbutton);
    23.             Debug.Log(PlayerPrefs.GetString("Audio"));
    24.             Debug.Log(PlayerPrefs.GetFloat("selectedbutton"));
    25.         }
    26.     }
     
  6. JuliusM

    JuliusM

    Unity Technologies

    Joined:
    Apr 17, 2013
    Posts:
    836
    Do you call PlayerPrefsSave() anywhere? instead of doing "paused = hasFocus;" in OnApplicationPause, you should simply call "PlayerPrefsSave();"