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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Scene volume does not reduce when master volume is lowered

Discussion in 'Scripting' started by phantom_x404, May 4, 2020.

  1. phantom_x404

    phantom_x404

    Joined:
    Apr 26, 2020
    Posts:
    26
    I have set a volume slider which is connected to the master volume, but it does not reduce the volume even if the slider is reduced or increased(even though in the audio mixer it shows master volume being reduced when the slider is moved) The code for slider is given below:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.Audio;
    5.  
    6. public class OptionsMenu : MonoBehaviour
    7. {
    8. public AudioMixer mixer;
    9.  
    10. public void SetLevel(float sliderValue)
    11.      {
    12.          mixer.SetFloat("MasterVolume", Mathf.Log10(sliderValue) * 20);
    13.      }
    14. }
    I have various scenes in the game, but I want this controller to reduce volume for all the scenes(it is the main volume controller in options menu). However it doesn't even reduce volume for the MainMenu scene which contains the options Menu. What should I do to resolve this?
    (please give me a beginner friendly solution)
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,724
  3. phantom_x404

    phantom_x404

    Joined:
    Apr 26, 2020
    Posts:
    26
    I am not using an audio listener in the game(or at least thats what I hope), however I have made an AudioManager which has all the audio clips stored(it is like the audio source function). So how do you suggest I set the Output property from the AudioManager script to use the mixer?
    The code for my audio manager script is below:
    Code (CSharp):
    1. using UnityEngine.Audio;
    2. using System;
    3. using UnityEngine;
    4.  
    5. public class AudioManager : MonoBehaviour
    6. {
    7.  
    8.     public Sound[] sounds;
    9.  
    10.     // Start is called before the first frame update
    11.     void Awake()
    12.     {
    13.         foreach (Sound s in sounds)
    14.         {
    15.             s.source = gameObject.AddComponent<AudioSource>();
    16.             s.source.clip = s.clip;
    17.  
    18.             s.source.volume = s.volume;
    19.             s.source.pitch = s.pitch;
    20.             s.source.loop = s.loop;
    21.         }
    22.     }
    23.  
    24.     void Start()
    25.     {
    26.         //Play("InGame");
    27.     }
    28.  
    29.     public void Play(string name)
    30.     {
    31.         Sound s = Array.Find(sounds, sound => sound.name == name);
    32.         if (s == null)
    33.         {
    34.             Debug.LogWarning("Sound: " + name + "not found!");
    35.             return;
    36.         }
    37.      
    38.         s.source.Play();
    39.      
    40.  
    41.      
    42.     }
    43.  
    44.     public void Pause(string name)
    45.     {
    46.         Sound s = Array.Find(sounds, sound => sound.name == name);
    47.         if (s == null)
    48.         {
    49.             Debug.LogWarning("Sound: " + name + "not found!");
    50.             return;
    51.         }
    52.  
    53.         s.source.Pause();
    54.     }
    55.  
    56.     public void Resume(string name)
    57.     {
    58.         Sound s = Array.Find(sounds, sound => sound.name == name);
    59.         if (s == null)
    60.         {
    61.             Debug.LogWarning("Sound: " + name + "not found!");
    62.             return;
    63.         }
    64.  
    65.         s.source.UnPause();
    66.     }
    67.  
    68. }
    69.  
    So what do you suggest?
    CORRECTION: I have added the audio listener to my Main Camera, and it was a really dumb thing to forget about it. In the Manual it only tells you that you can use the Audio Source to link it with Audio mixer, however there is no example given on how to do so, could you explain please?

    UPDATE: I got it, all you have to do is make the audio mixer as a public class and refer to it in the inspector, then you have to set the audio source to the audio mixer, both the codes are below:

    before the awake command(wherever you have marked your public and private values) add this:
    public AudioMixerGroup audioMixer;

    And then in the awake command add:
    s.source.outputAudioMixerGroup = audioMixer;




    Hope this helps you whoever is reading this in the future, Good Bye!
     
    Last edited: May 4, 2020
  4. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,724
    Like I said, you need to set the Output property of the AudioSource to your mixer. So in your AudioManager add a field for your mixer:
    Code (CSharp):
    1. public AudioMixerGroup mixerGroup;
    Assign your mixer asset to that field in the editor, then add to your Awake:
    Code (CSharp):
    1.         foreach (Sound s in sounds)
    2.         {
    3.             s.source = gameObject.AddComponent<AudioSource>();
    4.             s.outputAudioMixerGroup = mixerGroup;
    5.             // the rest...
    6.         }
     
    Last edited: May 4, 2020
  5. Karrzun

    Karrzun

    Joined:
    Oct 26, 2017
    Posts:
    123
    This might be a stupid idea but I usually connect my MasterVolume slider to my AudioListener volume. That field is static and can be accessed from anywhere and did the job for me so far.
    Is that a good idea? If not, could anybody explain to me why?