0
\$\begingroup\$

So far my mute button works, its just that I have to press my button twice (when game is restarted) in order for it to put my volume back to 1. Does anyone knows the problem. Anyway this is my script:

public bool mute; 


void Start()
{
    // check if the player has a music volume preference
    if (PlayerPrefs.HasKey("musicVolume"))
    {
        // if yes, apply it.
        GetComponent<AudioSource>().volume = PlayerPrefs.GetFloat("musicVolume");
    }
}


private void Muted ()
{
    AudioSource audioSource = gameObject.GetComponent<AudioSource>();

    mute = !mute;
    if (mute) {
        gameObject.GetComponent<AudioSource> ().volume = 0;
       // PlayerPrefs.SetFloat("mute", 0);
    }
    else
    {
        gameObject.GetComponent<AudioSource> ().volume = 1;
       // PlayerPrefs.SetFloat("mute", 1);
    }
    // write new music volume preference to persistent storage
    PlayerPrefs.SetFloat("musicVolume", audioSource.volume);
}

Thank you. :)

\$\endgroup\$
  • \$\begingroup\$ Because if you mute it, and it saves, you aren't saving the Bool. You're only saving the volume, not the check whether it's muted or not. \$\endgroup\$ – GMR516 Sep 14 '16 at 5:02
  • \$\begingroup\$ Thank you for your suggestion though \$\endgroup\$ – Bakausagi-chan Sep 15 '16 at 16:18
1
\$\begingroup\$

The reason is that you aren't setting the initial value for mute. Let's say you load up with the volume already muted (the float set to 0) as you described.

  1. The bool is initially defaults to false (not muted) so when you press space, it first mutes your volume (already muted, so no effect.) Boolean values default to false on declaration, as you did on the top of your code.
  2. In turn, you must press space again to go back unmuted.

I suggest putting this in your Start() method somewhere:

mute = (PlayerPrefs.GetFloat("musicVolume") == 0);

If the pref for volume is already muted, then it will set the initial value of bool to the appropriate value.

@smkplus also has a good implementation.

\$\endgroup\$
0
\$\begingroup\$

you shouldn't save volume value , you need save Boolean , because in your condition check boolean.so you can try this method for saving Boolean value.

using UnityEngine;
using System.Collections;

public class SoundManager : MonoBehaviour
{
    public bool mute;

    void Start()
    {
        //Get bool value
        mute = PlayerPrefs.GetInt("mute") > 0;
    }

    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space))
        {
            mute = !mute;
        }
        if (mute)
        {
            //Set bool value
            PlayerPrefs.SetInt("mute", mute ? 1 : 0);
            gameObject.GetComponent<AudioSource>().volume = 1;
        }
        else
        {
            //Set bool value
            PlayerPrefs.SetInt("mute", mute ? 1 : 0);
            gameObject.GetComponent<AudioSource>().volume = 0;
        }
    }
}
\$\endgroup\$
  • \$\begingroup\$ Thank you for your suggestion though \$\endgroup\$ – Bakausagi-chan Sep 15 '16 at 16:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.