PlayerPrefs, Saving Sound Preferences

Hey all,

Just working on my smartphone game and have hit a snag. I have what I think is functional code, but it’s not quite working how I intended.

It’s basically a “Mute” button that let’s you toggle between “audio.volume = 1” and “audio.volume = 0” states. I’ve managed to get to the point where I can save the state via PlayerPrefs.SetString and I can confirm that it is working via RegEdit(for checking my registry. Looking at Debugger, the problem is that my GetString is coming back as “” which sets my muted bool to false.

How can I set it up so that I can properly pull the value listed in my PlayerPrefs and then use it to set my sound preferences?

Here is the code:

using UnityEngine;

using System.Collections;

public class TitleScreen : MonoBehaviour {

public GUISkin titleSkin; 
public GUIStyle myStyle;

public bool muted;

public string sound;

//public GameObject titleMusic; 
		
// Use this for initialization
void Awake()
{
	SoundCheck();
}


void Start () 
{
	if(muted == false)
	{
		audio.volume = 1;
	}
	else if(muted == true)
	{
		audio.volume = 0;
	}
	audio.Play();
}

// Update is called once per frame
void Update () 
{
	
}


public void OnGUI()
{
	int buttonW = 100;
	int buttonH = 30;
	float screenW = Screen.width;
	float halfScreenW = Screen.width * 0.5f;
	


	GUI.skin = titleSkin;
	
	if (GUI.Button(new Rect(halfScreenW - (buttonW / 2), 350, buttonW, buttonH), "New Game"))
	{
		Application.LoadLevel("Stage Select");
	}
	
	if (muted == false)
	{
		if(GUI.Button(new Rect(halfScreenW - 15, 400, 30, 30), "X"))
		{
			audio.volume = 0;
			muted = true;
			PlayerPrefs.SetString("Sound", "muted");
		}
	}
		else if (muted == true)
	{
		if(GUI.Button(new Rect(halfScreenW - 15, 400, 30, 30), "0"))
		{
			audio.volume = 1;
			muted = false;
			PlayerPrefs.SetString("Sound", "enabled");
		}
	} 
}

public void SoundCheck()
{
	sound = PlayerPrefs.GetString("sound");
			
	if (sound == "enabled")
	{
		muted = false;
	}
	else if (sound == "muted")
	{
		muted = true;
	}
}

}

Thanks for your time! :smiley:

Found out what was wrong! My SoundCheck() method was asking for “sound” rather than “Sound”.

If you find yourself in the same boat, double check to make sure that everything is written exactly since PlayerPref names appear to be case sensitive!