About 'Set & Save Music Volume'!

In this video (objectives)...

  1. Use our PlayerPrefs functionality to connect the volume slider level with the saved volume level.
  2. Change level on the fly, use defaults button and save on exit for volume levels.

After watching (learning outcomes)...

Use PlayerPrefs and UI sliders to alter and save music volume levels.

(Unique Video Reference: 49_GL_CUD)

We would love to know…

  • What you found good about this lecture?
  • What we could do better?

Remember that you can reply to this topic, or create a new topic. The easiest way to create a new topic is to follow the link in Resources. That way the topic will…

  • Be in the correct forum (for the course).
  • Be in the right sub-forum (for the section)
  • Have the correct lecture tag.

Enjoy your stay in our thriving community!

Hello Rick, I have a question for you. I’m currently implementing the Volume control but I think I found an issue here. On the first part of the lesson you use a script TEST for setting the preference for the user and so after that the PlayerPrefs is created and accessible, but what if on fresh install ?
I modify the OptionsController script adding a method for get the default volume serialized there

public float GetDefaultVolume()
    {
        return defaultVolume;
    }

And after that I change the GetMasterVolume for check if the key of the PlayerPrefs exist yet or not.

 public static float GetMasterVolume()     
    {
        float value;

        if (PlayerPrefs.HasKey(MASTER_VOLUME_KEY))
        {
            Debug.Log("exist");
            value = PlayerPrefs.GetFloat(MASTER_VOLUME_KEY);
        }
        else
        {
            Debug.Log("not exist");
            value = FindObjectOfType<OptionsController>().GetDefaultVolume();
        }

        return value;
    }

Because otherwise at first load of the game from another computer the volume would be set to 0. Hope it’s a good solution :wink:

1 Like

Cool, looks like a good way to set the default volume.

Thank you Rick :slight_smile:

1 Like

Thank you Patrizio for posting this. I was so stuck trying to solve it in another way, that your code was the kickoff to solving it.

1 Like

Thanks for the sollution !

I would like to add something : when we start our game, on the splash screen, there is no instance of Options Controller, hence this line in “PlayerPrefsController” will not work :

value = FindObjectOfType<OptionsController>().GetDefaultVolume();

Instead, i recommend to put the method “GetDefaultVolume()” within the music player script, because the instance is created and never destroyed, so our PlayerPrefsController can find it like that :

value = FindObjectOfType<MusicPlayer>().GetDefaultVolume();

We then need to make little adjustments within our pieces of codes to make all the links work properly, and here we are, it’s working in a real case scenario (starting from splash screen). Hope this will help folks with same issue in the future :slight_smile:

1 Like

An even easier way to solve that without moving code around, and keeping the default volume neatly in the OptionsController would be to just create one in the method in PlayerPrefsController itself.

like this:

var  settings = new SettingsController();
return settings.GetDefaultVolume();

We already know the game is gonna start with the splash screen, meaning that the first time that GetMasterVolume() is called (when setting the volume in the Music Player on the splash screen), there’s not going to be an OptionsController anywhere.
And this way we never have to worry about whether or not there is one in the scene.

The default volume is also a constant value that the user can’t change, so we don’t have to worry about modifying it on any game objects.

The way my GetMasterVolume method looks like would be:

    public static float GetMasterVolume() {
        float volume;

        if (PlayerPrefs.HasKey(MASTER_VOLUME_KEY)) {
            volume = PlayerPrefs.GetFloat(MASTER_VOLUME_KEY);
        } else {
            var settings = new SettingsController();
            volume = settings.getDefaultVolume();
        }

        return volume;
    }

It will throw a warning that you can’t create a MonoBehaviour using the new keyword, however, it’ll still retrieve the value

Edit:
The latter can also be worked around by simply making the default volume and default difficulty static, and then making the get method for them static too. That allows to just call it directly.

    [SerializeField] static float defaultVolume = 0.7f;

    public static float getDefaultVolume() {
        return defaultVolume;
    }

and then call it as just:

volume = SettingsController.getDefaultVolume();
1 Like

Privacy & Terms