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. Dismiss Notice

Resolved Change Audio Mixer Volume with Slider

Discussion in 'Scripting' started by VengarlofForossa, Jun 11, 2023.

  1. VengarlofForossa

    VengarlofForossa

    Joined:
    Jan 1, 2023
    Posts:
    18
    Hello Everyone!

    I want to create three sliders so that the player can turn the volume down or up.

    To do this I have created a test scene and created:

    The first Slider to be the general volume.
    The second Slider to be the sound effects.
    The third Slider to be the music of the game.

    An Audio Mixer with two groups. When I press play, if I edit the Audio Mixer it works perfectly.
    But I can't get the volume of the audio mixer to go down with the Sliders.

    When I move the sliders I get the following message:

    "Exposed name does not exist: General".
    (I've also tried to set "GeneralVolume" and it doesn't work)

    I've been googling but I don't understand what's going on. In ExposedParameters in AudioMixer it says 0
    What is missing?

    Code (CSharp):
    1.  
    2. public class Options : MonoBehaviour
    3. {
    4.  
    5.     [SerializeField] private AudioMixer generalMixer;
    6.     [SerializeField] private AudioMixerGroup effectsMixerGroup;
    7.     [SerializeField] private AudioMixerGroup musicMixerGroup;
    8.  
    9.     // Start is called before the first frame update
    10.  
    11.     void Start()
    12.     {
    13.         Debug.Log(generalMixer.name);
    14.         Debug.Log(effectsMixerGroup.name);
    15.         Debug.Log(musicMixerGroup.name);
    16.  
    17.     }
    18.  
    19.     public void SetGeneral(float sliderValue)
    20.     {
    21.         generalMixer.SetFloat("General", Mathf.Log10(0.1f) * 20);
    22.     }
    23.  
    24.     public void SetEffects(float sliderValue)
    25.     {
    26.         effectsMixerGroup.audioMixer.SetFloat("Effects", Mathf.Log10(sliderValue) * 20);
    27.     }
    28.  
    29.     public void SetMusic(float sliderValue)
    30.     {
    31.         musicMixerGroup.audioMixer.SetFloat("Music", Mathf.Log10(sliderValue) * 20);
    32.     }
    33. }
    34.  


     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,563
    I've never used a mixer, but going from the documentation (hint hint hint!), the SetFloat() method operates on Parameters, just like the Animator.

    Your first screenshot shows zero parameters.

    Perhaps that would be a good place to begin your investigation?
     
  3. Hikiko66

    Hikiko66

    Joined:
    May 5, 2013
    Posts:
    1,300
    You need to expose the parameters that you want to access via script.
    Select the Master mixer
    In the inspector to the right, right click the volume and select Expose
    Click the ExposedParameters dropdown, it should have the new exposed parameter
    Right click it and select rename and name it something like "MasterVolume"

    generalMixer.SetFloat("MasterVolume", Mathf.Log10(0.1f) * 20);
     
    VengarlofForossa and Kurt-Dekker like this.
  4. VengarlofForossa

    VengarlofForossa

    Joined:
    Jan 1, 2023
    Posts:
    18
    Thank you very much!
    It was this part that was failing me. I had looked at the documentation but English is not my mother tongue and it was driving me crazy. I was at it for four hours until I decided to post and it went out of my head.
     
  5. CameronLewis

    CameronLewis

    Joined:
    Apr 17, 2016
    Posts:
    16
    I see where to right-click to "Expose 'Volume (of Master)' to script", but I can't seem to find where the ExposedParameters dropdown is. I don't suppose someone could direct me to whatever I need to select for it to appear in the Inspector?

    EDIT: Derp, found it. The instant I post about it, poof, it's plainly obvious and right in front of me. Been that kind of day. Ugh! :)