Search Unity

Resolved problem with changing volume with slider

Discussion in 'Scripting' started by aetztztztzzew, May 15, 2020.

  1. aetztztztzzew

    aetztztztzzew

    Joined:
    Apr 12, 2018
    Posts:
    39
    Hi! i hope this is right place to ask this question!
    I made slider that changes sound value and is connected to singleton soundManager
    script by onvalueChange(single) property
    Problem is i can change volume with it as long as i am on first scene(main menu), but when i leave that scene and return later to change volume i can move slider and slider value changes but volume is not affected.
    Any idea what should i look after?
    I dont think it will help but here is function i call by onValueChange event,
    I will provide more code if needed
    Code (CSharp):
    1.  public void setGlazbaLvl(float lvl)
    2.     {
    3.         mixerMaster.SetFloat("musicVol", Mathf.Log10(lvl) * 20);
    4.         PlayerPrefs.SetFloat("musicVol", lvl);
    5.     }
     
  2. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,836
    Where's the code that ultimately changes the volume on the AudioSource? How does that code know to run?
     
    aetztztztzzew likes this.
  3. aetztztztzzew

    aetztztztzzew

    Joined:
    Apr 12, 2018
    Posts:
    39
    here is whole soundManager
    Code (CSharp):
    1. public class zvukManager : MonoBehaviour
    2. {
    3.     // Start is called before the first frame update
    4.    public static zvukManager instance;
    5.  
    6.     public AudioMixer mixerMaster;
    7.  
    8.     public AudioSource pucanje;
    9.     public AudioSource sudar;
    10.     public AudioSource bgMusic;
    11.     public AudioSource klik;
    12.  
    13.     // Start is called before the first frame update
    14.  
    15.     private void Start()
    16.     {
    17.         float ll = PlayerPrefs.GetFloat("gameLvl");
    18.         setGameLvl(ll);
    19.  
    20.         float mll = PlayerPrefs.GetFloat("musicVol");
    21.         setGlazbaLvl(mll);
    22.     }
    23.  
    24.     private void Awake()
    25.     {
    26.         if (instance == null)
    27.         {
    28.             instance = this;
    29.         }
    30.         else if (instance != null )
    31.         {
    32.             Destroy(this.gameObject);
    33.  
    34.         }
    35.         DontDestroyOnLoad(this);
    36.  
    37.  
    38.  
    39.     }
    40.  
    41.  
    42.     public void PlaySingle(AudioClip clip)
    43.     {
    44.         //Set the clip of our efxSource audio source to the clip passed in as a parameter.
    45.        klik.clip = clip;
    46.  
    47.         //Play the clip.
    48.         klik.PlayOneShot(klik.clip);
    49.     }
    50.    
    51.     public void setGameLvl(float lvl)
    52.     {
    53.         mixerMaster.SetFloat("gameVol", Mathf.Log10(lvl)*20);
    54.         PlayerPrefs.SetFloat("gameVol", lvl);
    55.         PlayerPrefs.SetFloat("gameLvl", lvl);
    56.     }
    57.     public void setGlazbaLvl(float lvl)
    58.     {
    59.         mixerMaster.SetFloat("musicVol", Mathf.Log10(lvl) * 20);
    60.         PlayerPrefs.SetFloat("musicVol", lvl);
    61.     }
    62.     public void startGameLevel()
    63.     {
    64.         float param = PlayerPrefs.GetFloat("gameVol");
    65.         mixerMaster.SetFloat("gameVol", param);
    66.     }
    67.     public void startGlazbaLvl()
    68.     {
    69.         float param = PlayerPrefs.GetFloat("musicVol");
    70.         mixerMaster.SetFloat("musicVol", param);
    71.     }
     
  4. aetztztztzzew

    aetztztztzzew

    Joined:
    Apr 12, 2018
    Posts:
    39
    These images show onvalueComponentFromSlider before and after changing scenes
    Captureprije.PNG Captureslider.PNG
     
  5. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,836
    I'm guessing you have that pointing to a zvukManager in the same scene? But you have zvukManager set up so that the first one will stick around between scenes, and when you reload the scene, the copy in the newly-loaded scene will see that one already exists and destroy itself. Hence breaking all saved references to it.

    Create a new script on your slider, have your slider call that script instead of calling the zvukManager, and then make the function that script use zvukManager.instance to find the correct zvukManager to call.
     
    Joe-Censored and aetztztztzzew like this.
  6. aetztztztzzew

    aetztztztzzew

    Joined:
    Apr 12, 2018
    Posts:
    39
    You solved it! thanks