Search Unity

Volume Slider Not Working

Discussion in 'Scripting' started by DaRealMik, Jan 18, 2020.

  1. DaRealMik

    DaRealMik

    Joined:
    Jul 27, 2019
    Posts:
    69
    I have a slider that controls my volume. It was working fine until I imported the Unity Particle Pack off the Asset Store. The slider doesn't update the mixer's value, but does everything else. My code was unchanged and yes, I have assigned my audio mixer, functions, and all of that. I'm not sure what happened, please help!
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.Audio;
    3. using UnityEngine.UI;
    4.  
    5. public class OptionsMenu : MonoBehaviour {
    6.  
    7.     public AudioMixer mixer;
    8.     public Slider slider;
    9.  
    10.     public void SetVolume(float volume)
    11.     {
    12.         mixer.SetFloat("Volume", volume);
    13.     }
    14.  
    15.     public void Start()
    16.     {
    17.         SetVolume(PlayerPrefs.GetFloat("Volume", 0));
    18.         slider.value = PlayerPrefs.GetFloat("Volume");
    19.     }
    20.  
    21.     public void OnDisable()
    22.     {
    23.         float Volume = 0f;
    24.  
    25.         mixer.GetFloat("Volume", out Volume);
    26.  
    27.         PlayerPrefs.SetFloat("Volume", Volume);
    28.         PlayerPrefs.Save();
    29.     }
    30.  
    31.  
    32.  
    33.  
    34. }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,752
    EDIT to this old thread I posted... see here for more on dynamic Slider values:

    https://forum.unity.com/threads/cant-return-a-value-for-drop-down-list.1171442/#post-7504738

    ---------------------- my original post:

    This is kind of an annoying tricky part about Sliders. The callback shouldn't take a float unless you want a constant float.

    Instead you want it to take a
    Slider
    , from which you pry out the
    .value
    field. It is discussed here:

    https://answers.unity.com/questions/1069374/slider-onvaluechanged-sending-only-0-or-other-defi.html

    I just tested it here with this code:

    Code (csharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3.  
    4. public class glug : MonoBehaviour {
    5.  
    6.     public void SetVolume( Slider s)
    7.     {
    8.         Debug.Log( "fnord:" + s.value);
    9.     }
    10. }
    and my slider setup event looked like:

    Screen Shot 2020-01-19 at 1.52.44 PM.png

    Don't forget to drag the slider instance itself into argument field.
     
    Last edited: May 10, 2023
    koeningsegg100 likes this.