Search Unity

Audio Manage the volume through different scenes.

Discussion in 'Audio & Video' started by casiantumblr, Apr 15, 2018.

  1. casiantumblr

    casiantumblr

    Joined:
    Mar 20, 2018
    Posts:
    4
    Hi everyone, I'm not confident with audio settings in unity. I need to set the master volume by a slider in an options menu and keep it the same for every level, I've tried this code that changes the master volume in the Mixer but it doesn't work when I load another scene. In this scene I have some audio sources attached to a game object and managed by another script. Any suggestions?
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.Audio;
    5. public class SettingsMenu : MonoBehaviour {
    6.  
    7.     public AudioMixer audioMixer;
    8.    
    9.     public void SetVolume(float Volume){
    10.        
    11.         audioMixer.SetFloat ("Volume", Volume);
    12.     }
    13.  
    14. }
     
  2. Deejayfistfang

    Deejayfistfang

    Joined:
    Dec 4, 2017
    Posts:
    8
    Hey casiantumblr.

    What you can do (what I do) is in your first scene (usually the menu but can be a loading screen) is have a script (Iike your SettingsMenu script) that gets previous games saved preferences. If your using a slider to change volume, lets assume, then your settingsmenu script will not only change the volume, but SAVE it in preferencs and so when the scene is reloaded or a new one open, the script applies it automatically. Like this


    Code (CSharp):
    1. public Slider m_volume;
    2.  
    3. void Awake(){
    4.  
    5.  
    6. if(m_slider!=null && PlayerPrefs.HasKey("volume"){
    7.  
    8. // get temporary float
    9. float wantedVolume=PlayerPrefs.GetKey("volume",1f);
    10.  
    11. // this is IMPORTANT I find because it update your slider because it will naturally start at 0 even when in a previous game you set it to whatever
    12.  
    13.             m_slider.value = wantedVolume;
    14. // this actually changes all game sounds volume
    15.             AudioListener.volume = prefGet;
    16.  
    17. //this adds a listener
    18.             m_slider.onValueChanged.AddListener(delegate { SetGameVolume(m_slider.value); });
    19.  
    20. }


    }

    now make this method

    Code (CSharp):
    1.     public void SetGameVolume(float volume) {
    2. // update volume again when slider changes
    3.         AudioListener.volume = volume;
    4.  
    5. // this SAVES the incoming slider change for next time
    6.         PlayerPrefs.SetFloat("volume", volume);
    7.  
    8.  
    9.     }

    So NOW that we do that, drag your in game slider (make sure its range it 0 - 1) into the public m_slider slot and it should work, as long your scene has the SettingsMenu in it.
     
    unity_17onu8L_jjTy5g likes this.
  3. Alvarezmd90

    Alvarezmd90

    Joined:
    Jul 21, 2016
    Posts:
    70
    Sounds like you still need the music player to be an persistent manager. instanciate a music game object from another manager, load it from the 'assets'resources/prefabs' folder. Then set it to DontDestroyOnLoad. Use a tag like 'music' to make every object be able to find the gameObject's id. Now the music played will transfer between scenes because it carries through and doesn't get destroyed. Every object can refer to the music object's AudioSource parameters doing a GameObject.FindGameObjectWithTag codeline.
     
    Last edited: May 12, 2018
  4. keblight

    keblight

    Joined:
    May 27, 2015
    Posts:
    34
    I am having similar issues...thanks to all for the replies.
     
    Hamza-Br likes this.
  5. adhe_maul

    adhe_maul

    Joined:
    Jun 12, 2019
    Posts:
    1
    AudioListener.volume = prefGet;
    From where variabel (prefGet) or there is methode else ?