0
\$\begingroup\$

How do I save volume of my audio source (clicking the mute button) when game is restarted. You see I have one scene in my game and once the player dies there is a button the user can click on that resets the game, however it also resets the audio source, so when I play the game and click my mute button, the audio source volume is at 0 but when I die and click the reply button (the restart/reset button) the music is restarted as well, instead of staying muted (save the current volume). I tried using DontDestroyOnLoad(this) but my muted script is attached to my game over panel animation (the panel comes down when player dies) making the my game over panel animation not being destroyed when you restart the game. I tried other methods like putting my mute button script on a parent empty object and putting the object that has the audio source on under it as a child but it did the same thing. Anyway this is my mute button script:

private bool mute; 

public void Muted ()
{
    mute = !mute;
    if (mute){
        gameObject.GetComponent<AudioSource>().volume = 0;
    }else
    {
        gameObject.GetComponent<AudioSource>().volume = 1;
    }
  }
}

And this is my restart level when button is pressed (p.s. I used the OnClick method activating methods):

public void NavigateTo(int scene)
{
    Application.LoadLevel ("Game Level");
}

Thank you in advance :)

\$\endgroup\$

2 Answers 2

1
\$\begingroup\$

Adjusting the volume of the background music (or muting it altogether) is a typical player preference. As such, it should be saved in the PlayerPrefs class. PlayerPrefs is a key/value store. It allows you to store and retrieve variables (int, float or string) under string-keys of your choice. It uses a platform-appropriate persistent storage method, so it doesn't just survive scene reloads but also closing and relaunching the whole game application.

To use the player preference in your audio source, add a script to it which checks if a preference exists and if so apply it in the Start method. When you change the music volume, also change the corresponding value in PlayerPrefs:

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 bool mute; 

public void ToggleMute ()
{        
    AudioSource audioSource = gameObject.GetComponent<AudioSource>();

    mute = !mute;
    if (mute){
        audioSource.volume = 0.0f;
    }else
    {
        audioSource.volume = 1.0f;
    }

    // write new music volume preference to persistent storage
    PlayerPrefs.SetFloat("musicVolume", audioSource.volume);

  }
}
\$\endgroup\$
3
  • \$\begingroup\$ Thank you, I tried your suggestion out but it's just setting my volume to 1. (added a float value to 1). Unless if I did something wrong. :(. Also what's musicVolume?? \$\endgroup\$ Commented Aug 26, 2016 at 7:19
  • \$\begingroup\$ @JesscaStone "musicVolume" is a key I made up to give that preference a name to store it under in PlayerPrefs. PlayerPrefs is a key/value store where you can store variables under arbitrary keys. \$\endgroup\$
    – Philipp
    Commented Aug 26, 2016 at 8:33
  • \$\begingroup\$ @JesscaStone I rewrote the example in my answer to be based on the snippet from your question. I hope this makes it clearer how to use PlayerPrefs. \$\endgroup\$
    – Philipp
    Commented Aug 26, 2016 at 8:39
0
\$\begingroup\$

Instead of setting the volume of the AudioSource, you can play around with its mute field.

public bool mute{
        set{
            PlayerPrefs.SetFloat("muted", value ? 1 : 0);
        }
        get{
            return PlayerPrefs.GetFloat("muted", 0) == 0 ? false : true;
        }
}

So in your Muted method, you could just have 2 lines of code

mute = !mute;
    gameObject.GetComponent<AudioSource>().mute = mute;

But when you reset the level, you will lose the settings. So you want to set the mute value in Start or Awake as well

 gameObject.GetComponent<AudioSource>().mute = mute;

This will be a better option that setting the volume because you could have values in between 0 and 1 as well. By your method, you could only have no volume or maximum value.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .