GetMasterVolume() returns strange number

Hi there, I was trying to set up the volume for my game but it seems like my GetMasterVolume() method returns 0,06900791. Because of this, when I hit play the music is very low and the slider is set to that number. I tried to put a default value in the .GetFloat but I doesn’t work for me…

Any ideas?

Thanks

Hi John,

Have you already tried to add Debug.Logs to your code to see what is going on during runtime?

Hi Nina,

I added a Debug.log in the OptionsController.cs so I can see what the GetMasterVolume() method was returning, yet I can’t see where the problem is coming from. Here are the two scripts I’m using:

OptionsController.cs

public class OptionsController : MonoBehaviour
{
    [SerializeField] Slider volumeSlider;
    [SerializeField] float defaultVolume = 0.8f;

    // Start is called before the first frame update
    void Start()
    {
        Debug.Log("Master volume is " + PlayerPrefsController.GetMasterVolume());
        volumeSlider.value = PlayerPrefsController.GetMasterVolume();
    }

    // Update is called once per frame
    void Update()
    {
        var musicPlayer = FindObjectOfType<MusicPlayer>();
        if(musicPlayer)
        {
            musicPlayer.SetVolume(volumeSlider.value);
        }
        else
        {
            Debug.LogWarning("No music player found");
        }
    }

    public void SaveAndExit()
    {
        PlayerPrefsController.SetMasterVolume(volumeSlider.value);
        FindObjectOfType<LevelLoader>().LoadMainMenu();
    }

    public void DefaultSettings()
    {
        volumeSlider.value = defaultVolume;
    }
}

PlayerPrefsController.cs

public class PlayerPrefsController : MonoBehaviour
{
    const string MASTER_VOLUME_KEY = "master volume";
    const string DIFFICULTY_KEY = "difficulty key";

    const float MIN_VOLUME = 0f;
    const float MAX_VOLUME = 1f;

    public static void SetMasterVolume(float volume)
    {
        if (volume >= MIN_VOLUME && volume <= MAX_VOLUME)
        {
            Debug.Log("Master volume set to " + volume);
            PlayerPrefs.SetFloat(MASTER_VOLUME_KEY, volume);
        }
        else
        {
            Debug.LogError("Master volume is out of range");
        }
    }

    public static float GetMasterVolume()
    {
        return PlayerPrefs.GetFloat(MASTER_VOLUME_KEY, 1);
    }
}

I set a default value of 1f in the GetMasterVolume() but the thing is when I debug it shows a value of 0,06900791.

Try to add the f for float behind the 1.

PlayerPrefs.GetFloat(MASTER_VOLUME_KEY, 1f);

Given that the Start method in the OptionsController gets called before the SetMasterVolume method, the console should say “Master volume is 1”.

Double check the references. Does the volumeSlider variable reference the correct slider in your game? Do the game objects have the relevant components attached? Does no other component/object call GetMasterVolume()? Add a Debug.Log to GetMasterVolume to see how often that method gets called.

For testing purposes, call PlayerPrefs.DeleteAll() in the Awake method of the OptionsController. Maybe you are just getting a value from the old PlayerPrefs file, which is stored somewhere on your harddrive. If there is a key named “master volume”, the GetFloat() method returns the value associated with that key, else 1f.

1 Like

Calling PlayerPrefs.DeleteAll() in the Awake fixed the problem! I just remembered that I had an older script with a key called “master volume” so that was the mistake.

Thank you Nina, you’re amazing!

I’m glad the solution was so simple. Don’t forget to remove that line. :wink:

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms