- Home /
PlayerPrefs for Mute/Unmute button
Hello I'm linking together separate buttons to mute and unmute audio. Here's the mute script: Updated
var mySkin : GUISkin;
function OnGUI () {
GUI.skin = mySkin;
if (GUI.Button (Rect (10, 550, 65, 35), "Mute")) {
audio.mute = true;
var script1 = GetComponent("UnMuteButton");
script1.enabled = true;
var script2 = GetComponent("MuteButton");
script2.enabled = false;
PlayerPrefs.SetInt("AudioIsMuted", 1);
}
}
And here's the unmute script:
GetComponent("UnMuteButton").enabled = false;
var mySkin : GUISkin;
function OnGUI () {
GUI.skin = mySkin;
if (GUI.Button (Rect (10, 550, 65, 35), "Unmute")) {
audio.mute = false;
var script1 = GetComponent("UnMuteButton");
script1.enabled = false;
var script2 = GetComponent("MuteButton");
script2.enabled = true;
}
}
When the Mute button is clicked the audio becomes muted and the button is replaced by an Unmute button, and vice versa. What I'd like to know is how can I use PlayerPrefs to save whether or not the Player has muted or unmuted the audio and access their preferences whenever they return? I understand a little about PlayerPrefs but not a whole lot.
Thank you for any answers or feedback, -Ben
Update I've updated the mute code, adding `PlayerPrefs.SetInt("AudioIsMuted", 1);`
I'm using
function Update(){
var audioIsMuted = PlayerPrefs.GetInt("AudioIsMuted") ==1;
}
To load the player's preference but I still can't get it to work.
Answer by DannyB · Nov 24, 2012 at 10:01 AM
Use PlayerPrefs.SetInt, like this:
When the user clicks:
PlayerPrefs.SetInt( "AudioIsMuted", 1 ); // for mute
// or
PlayerPrefs.SetInt( "AudioIsMuted", 0 ); // for unmute
When you want to check the status on load:
bool audioIsMuted = PlayerPrefs.GetInt( "AudioIsMuted" ) == 1;
Thanks for your answer. I incorporated your SetInt code and placed your GetInt code into another script called 'Get$$anonymous$$utePreferences'. The inspector is saying to insert a semicolon at the end of 'boolean'. I did this but then it becomes an error.
Ahm.... what is the line of code and what is the exact error?
Sorry, should've been more specific. The line of code is: bool audioIs$$anonymous$$uted = PlayerPrefs.GetInt("Audio$$anonymous$$uted") == 1; The console said to insert a semicolon at the end of 'bool' which I did but now it's declaring that 'bool' is an unknown identifier.
Oh - my code is in C#, just use the javascript way of declaring a variable. var audioIs$$anonymous$$uted = ...
Your answer
Follow this Question
Related Questions
Temporarily disable gamepad button 1 Answer
Unity - keep created buttons after quit 1 Answer
Find Location via script query 0 Answers
Interactable not save PlayPrefs 1 Answer
Editing an XML File 1 Answer