Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question Set the value of an audiomixergroup to the value of a saved playerpref

Discussion in 'Audio & Video' started by LifeSymbiont, Nov 26, 2021.

  1. LifeSymbiont

    LifeSymbiont

    Joined:
    Apr 19, 2015
    Posts:
    36
    I'm trying to code a saving function for my Music group and Sound group sliders. So the player does not have to set them right every time the game starts.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5. using UnityEngine.Audio;
    6.  
    7. public class MusicAndSoundVolumeChangeSetting : MonoBehaviour
    8. {
    9.     public AudioMixer mixer;
    10.     void Start()
    11.     {
    12.      
    13.         PlayerPrefs.GetFloat("MusicVol");
    14.         mixer.SetFloat("PrefMusicVol", MusicSliderValue);
    15.  
    16.         PlayerPrefs.GetFloat("SoundVol");
    17.         mixer.SetFloat("PrefSoundVol", SoundSliderValue);
    18.      
    19.     }
    20.  
    21.     public void ChangeMusicVolume(float MusicSliderValue)
    22.     {
    23.         mixer.SetFloat("MusicVol", MusicSliderValue);
    24.         PlayerPrefs.SetFloat("PrefMusicVol", MusicSliderValue);
    25.     }
    26.  
    27.     public void ChangeSoundVolume(float SoundSliderValue)
    28.     {
    29.         mixer.SetFloat("SoundVol", SoundSliderValue);
    30.         PlayerPrefs.SetFloat("PrefSoundVol", SoundSliderValue);
    31.     }
    32. }
    33.         /*
    34.         if(!PlayerPrefs.HasKey("MusicVol"))
    35.         {
    36.             PlayerPrefs.SetFloat("MusicVol", 1);
    37.         }
    38.  
    39.         else
    40.         {
    41.             LoadMusic();
    42.         }
    43.      
    44.         if(!PlayerPrefs.HasKey("SoundVol"))
    45.         {
    46.             PlayerPrefs.SetFloat("SoundVol", 1);
    47.         }
    48.  
    49.         else
    50.         {
    51.             LoadSound();
    52.         }
    53.         */
    I don't know how to set this right. Everything else works. Just the Start function needs the right code.
    Please help me.
     
    Last edited: Nov 26, 2021
  2. LifeSymbiont

    LifeSymbiont

    Joined:
    Apr 19, 2015
    Posts:
    36
  3. Serge_Billault

    Serge_Billault

    Joined:
    Aug 26, 2014
    Posts:
    190
    There are strange discrepencies in your code, Cortex00, for exemple:

    * At line 13: you use the key "MusicVol" to get the player pref value.
    * At line 24: you use the key "PrefMusicVol" to set the player pref value.
    > The key for accessing the player pref vol property do not change over time, it's either "MusicVol" or "PrefMusicVol".

    * At line 14: you use the key "PrefMusicVol" to set the Mixer value.
    * At line 23: you use the key "MusicVol" to set the Mixer value.
    > The key for accessing the Mixer vol property do not change over time, it's either "MusicVol" or "PrefMusicVol".

    Then in the Start function there is the fact that for exemple you do not store the result of PlayerPrefs.GetFloat("MusicVol"); into a variable that can be assigned to the Mixer:

    * You have:
    PlayerPrefs.GetFloat("MusicVol");
    mixer.SetFloat("PrefMusicVol", MusicSliderValue);
    > MusicSliderValue never took the value of PlayerPrefs.GetFloat("MusicVol");

    * It should be:
    float MusicVol = PlayerPrefs.GetFloat( <your pref music vol key> );
    float normalized_vol_converted_to_db = ConvertToDB(MusicVol);
    mixer.SetFloat( <your Mixer music vol key>, normalized_vol_converted_to_db);
    your_slider.value = MusicVol;
    > the player pref value is this time really used to setup the mixer and slider values.
     
    Last edited: Nov 28, 2021
  4. LifeSymbiont

    LifeSymbiont

    Joined:
    Apr 19, 2015
    Posts:
    36
    Thank you allot for your Feedback, thanks to you I was capable to fix my code and got it running as intended.
    Thank you so much!