Using PlayerPrefs for Mute/Unmute button problem

Hello I’m linking together separate buttons to mute and unmute audio. When the Mute button is clicked the audio becomes muted and the button is replaced by an Unmute button, and vice versa.

Here’s the mute script:

var mySkin : GUISkin;
function OnGUI () {
GUI.skin = mySkin;
     if (GUI.Button (Rect (10, 555, 65, 35), "Mute")) {
			audio.mute = true;  
			var script1 = GetComponent("UnMuteButton"); 
    		script1.enabled = true;
    		var script2 = GetComponent("MuteButton"); 
    		script2.enabled = false;
    		PlayerPrefs.SetInt("AudioIsMuted", 1); 
    } 
}

I’ve tried the line ‘PlayerPrefs.SetInt(“AudioIsMuted”, 1);’ to set the players preference.

And here’s the unmute script:

GetComponent("UnMuteButton").enabled = false;
var mySkin : GUISkin;
function OnGUI () {
GUI.skin = mySkin;
     if (GUI.Button (Rect (10, 555, 65, 35), "Unmute")) {
     		audio.mute = false;
            var script1 = GetComponent("UnMuteButton"); 
    		script1.enabled = false;
    		var script2 = GetComponent("MuteButton"); 
    		script2.enabled = true;
    		PlayerPrefs.SetInt("AudioIsMuted", 2);    			
    } 
}

This time using ‘PlayerPrefs.SetInt(“AudioIsMuted”, 2);’ to set the other preference.

And finally this is the script I’m trying to use to retrieve the PlayerPrefs, I’ve name it CheckMutePreferences:

var audioIsMuted: int;
function Update(){

	audioIsMuted = PlayerPrefs.GetInt("AudioIsMuted");

}

The problem is that it’s simply not working. In the inspector the audioIsMuted var corresponds with whether or not the audio is muted, or unmuted. But when I, for example, click on mute and the unmute button appears and then I change scenes and return I find that it’s no longer muted, and the unmute button isn’t displayed. I’m not sure where I’m going wrong.

Thank you for any answers or feedback, -Ben

I really dont get your script, why do you have 3 scripts for that little work? Do it like:

#pragma strict

public var t1 : Texture2D; //UnMuted button texture
public var t2 : Texture2D; //Muted button texture

private var muted = true;

private var ctext : Texture2D;

function Start () {
	var res = PlayerPrefs.GetInt("muted");
	if ( res == 1 )
		muted = true;
	else
		muted = false;
}

function OnApplicationQuit(){
	var res = 0;
	if ( muted )
		 res = 1;
	PlayerPrefs.SetInt("muted", res);
}

     
function OnGUI() {
	if ( muted ) 
		ctext = t1;
	else
		ctext = t2;
		
	if ( GUI.Button( Rect( 70, 70, 40, 40 ), ctext ) ) {
		muted = !muted;
	}

        //in this case the transform is actualy the audio source of the background music, so I just did this, but if you want to do something else then put 

        // if ( muted ) {
              //NOT Muted!! Yea I made it a little bit confusing, but this part is for NOT muted actions
        //} else {
             //muted
        //}

	transform.audio.enabled = muted;
}

Hello Ben. GetInt should not be called in update as it is constantly assigning the value of the pref to the var. Start function is a good spot for assigning when the new scene is opened. You can use the loaded pref value as a condition for showing and hiding the other button so you don’t have to use a separate script. Unless that’s what your going for, lol.

You need to use PlayerPrefs.Save at some point to actually save the values.

I’ve been using player prefs for years now and never used that line.

Check my version

One less function - because it saves everytime the button is pushed

Two less conditions being checked at all times. :wink: :sunglasses:

var mState : int;
var muted : boolean;
var aState : String = "Mute";
private var aSource : AudioSource;
///===================================================//
function Start() {
    //------------//
    if(GetComponent(AudioSource)) {
      aSource = GetComponent(AudioSource);
    }
    //------------//
    mState = PlayerPrefs.GetInt("muted");
    //------------//
    if((mState==0)(aSource)) {
      aState = "Mute";  aSource.mute = false;
    }
    else {
      aState = "UnMute"; aSource.mute = true;
    }
    //------------//
}
///=========================================================//
function OnGUI() {
    //------------//    
    if(PlayerPrefs.GetInt("muted")==0) { aState = "Mute"; muted = false; }
    else { aState = "UnMute"; muted = true; }
    //------------//
    if(GUI.Button( Rect( 70, 70, 80, 40 ), aState) ) {
      //------------//
      muted = !muted;
      //------------//
      if((muted)(aSource)) {
        mState = 1; aSource.mute = true;
      }
      else {
        mState = 0; aSource.mute = false;
      }
      //------------//
      PlayerPrefs.SetInt("muted", mState);
    }
    //------------//
}
///=========================================================//

Thank you very much. I added your script and then added an Audio Source after it, in my case a song, however it’s not being muted when I click on Mute.

I added the audioSource lines to my original example. notice that I avoid null reference exceptions by asking if the audioSource reference is not null.

Best of luck :slight_smile:

Thank you very much, all is well :slight_smile: