Save Sound/Music Settings With Player Prefs?

Hello, I’m trying to save my sound/music settings with Player Prefs after I either turn off the sound/music, or turn it on. So far I haven’t had any luck in doing so. I’ve used Player Prefs before to save my high score, but now I want to save my sound/music too. So how can I do this? Here’s my sound/music menu script to where you can either turn it on or off:

public class SoundAndMenu : MonoBehaviour {

    public Texture backgroundTexture;
   
    public float guiPlacementY1;
    public float guiPlacementY2;
    public float guiPlacementY3;
   
    public float guiPlacementX1;
    public float guiPlacementX2;
    public float guiPlacementX3;

   
   
   
    void OnGUI(){
                // Display our background texture
                GUI.DrawTexture (new Rect (0, 0, Screen.width, Screen.height), backgroundTexture);
       
                // Display our Buttons
                if (GUI.Button (new Rect (Screen.width * guiPlacementX1, Screen.height * guiPlacementY1, Screen.width * .5f, Screen.height * .1f), "On")) {
                        AudioListener.volume = 100;
                        AudioManager.AudioInstance.PlaySound ("Sound");
                        print(PlayerPrefs.GetInt("On"));
                        print ("Ckicked Play Game");
           
                }
                if (GUI.Button (new Rect (Screen.width * guiPlacementX3, Screen.height * guiPlacementY3, Screen.width * .5f, Screen.height * .1f), "Off")) {
                        AudioListener.volume = 0;
                        print(PlayerPrefs.GetInt("Off"));
                        print ("Clicked Play Game");
                        //Application.LoadLevel("SoundMenu");
                }
                if (GUI.Button (new Rect (Screen.width * guiPlacementX2, Screen.height * guiPlacementY2, Screen.width * .3f, Screen.height * .1f), "Back")) {
                        AudioManager.AudioInstance.PlaySound ("Back");
                        PlayerPrefs.Save();
                        print ("Clicked Play Game");
                        Application.LoadLevel ("OptionsMenu");
       
                }
        }
}

As you can tell, I already started messing around with the Player Prefs for this, but no luck. This was made early a while back so the usage of Player Prefs is wrong. Anyways thanks for reading.

The main problem is that you are getting an int from 0 to 100, while the volume of the AudioListener uses a float from 0.0 to 1.0.

But need some info before suggesting anything, why you are trying to get two different Prefs for what it looks like a toggle, you got there On and Off, while you can just have one single toggle and save that instead.

Here what it could be done to simplify it if I understood right and you are using the Pref as a switch:

using UnityEngine;

public class SoundAndMenu : MonoBehaviour {
   
   public Texture backgroundTexture;
   
   public float guiPlacementY1;
   public float guiPlacementY2;
   public float guiPlacementY3;
   
   public float guiPlacementX1;
   public float guiPlacementX2;
   public float guiPlacementX3;

   void Start (){
    //Make sure the entry on the Prefs exist or proceed to create it
     if(PlayerPrefs.HasKey("AudioToggle")) {
  //Check the value of the Pref as a toggle (1 = max volume, 0 = off)
       AudioListener.volume = (PlayerPrefs.GetInt("AudioToggle", 1) == 1) ? 1.0f : 0.0f;
     }   
     else
     {
       AudioListener.volume = 1.0f;
       PlayerPrefs.SetInt ("AudioToggle", 1);
       PlayerPrefs.Save();
     }
   }
   
   void OnGUI(){
     // Display our background texture
     GUI.DrawTexture (new Rect (0, 0, Screen.width, Screen.height), backgroundTexture);
     
     // Display our Buttons
     if (GUI.Button (new Rect (Screen.width * guiPlacementX1, Screen.height * guiPlacementY1, Screen.width * .5f, Screen.height * .1f), "On")) {
       AudioListener.volume = 1.0f;
       AudioManager.AudioInstance.PlaySound ("Sound");
  //Set the Preference
       PlayerPrefs.SetInt("AudioToggle", 1);
       print(PlayerPrefs.GetInt("AudioToggle"));
       print ("Ckicked Play Game");
       
     }
     if (GUI.Button (new Rect (Screen.width * guiPlacementX3, Screen.height * guiPlacementY3, Screen.width * .5f, Screen.height * .1f), "Off")) {
       AudioListener.volume = 0;
  //Set the Preference
       PlayerPrefs.SetInt("AudioToggle", 0);
       print(PlayerPrefs.GetInt("AudioToggle"));
       print ("Clicked Play Game");
       //Application.LoadLevel("SoundMenu");
     }
     if (GUI.Button (new Rect (Screen.width * guiPlacementX2, Screen.height * guiPlacementY2, Screen.width * .3f, Screen.height * .1f), "Back")) {
       AudioManager.AudioInstance.PlaySound ("Back");
       PlayerPrefs.Save();
       print ("Clicked Play Game");
       Application.LoadLevel ("OptionsMenu");
       
     }
   }
}

Yes this is exactly what I wanted, thank you! But hold on here I’m not just going to thank you then copy your code and leave, I need to know some things. First off I didn’t know AudioListener uses a float from 0.0 to 1.0, I got the 100 and 0 from a different post about turning the sound and music off. Second well there is no second. Okay so question 1, do you use haskey for all buttons containing some Player Prefs. Question 2: Will this work for IOS(the button clicking). Finally Question 3: Is there some kind of set pattern for Player Prefs? Like for an example GetInt, then SetInt, then finally Save.

To answer your question, I wasn’t thinking when I wrote that code, and I’m not that familiar with Player Prefs. :face_with_spiral_eyes:

HasKey is used once because is a simple check to make sure the Pref entry it exists. On the code void Start I posted you see I check if the entry exist, the entry name is given by what you have set already with SetInt with the string.

HasKey(“AudioToggle”) = check if there is any pref with the id of AudioToggle. If it doesn’t exist, in the code I made it create a default of 1 (meaning audio is on), and then save the prefs.

So after that every time you run the game will check for that entry on the prefs, the first time will set a default one, all the other times will read it as you set it.

About Set and Get, is what their name suggest, Get is about getting the value, so in your case you get the value of the entry AudioToggle, which is 1 or 0 in our case, Set instead will set again the int to a given value, for this the Set function need two arguments, in your case:

SetInt(entryname, int value), as you can see in the code I wrote I set on the “On” button to 1, and on the “Off” button to 0.

Another thing about the code I put on Start, you can see this line:

AudioListener.volume = (PlayerPrefs.GetInt(“AudioToggle”, 1) == 1) ? 1.0f : 0.0f;

This will run if the pref of the audio already exist, all it does is, if the Pref is = 1 then set AudioListener to 1.0, else will set to 0.0. This way your gui button need only to save the pref, at each game run it will be read automatically just once and you are good to go.

So in your code unless you need the prints, you can just delete all the GetInt you put there, is only needed once at Start.
As about IOS I can’t help there as I have no device with it, but I’m sure PlayerPrefs saves there too.

Thank you for the explanation. I think I’m starting to understand it more. No worries about the IOS, I’m also sure that PlayerPrefs saves there too. Thanks again.