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

Changing AudioListener at runtime not working:

Discussion in 'Scripting' started by Jeepster, Sep 24, 2020.

  1. Jeepster

    Jeepster

    Joined:
    Jan 23, 2014
    Posts:
    401
    Hi,

    I read in the forums that you can change the overall audio in your scene with the following code:

    public void ChangeVol(float newValue)
    {
    float newVol = AudioListener.volume;
    newVol = newValue;
    AudioListener.volume = newVol;
    }


    And then you add a slider component to a canvas and hook up the slider to the "ChangeVol" function, but what happens here is thatas soon as I touch the slider it changes to a fixed value set in the inspector, it doesn't follow the slider up or down.

    I also tried using player prefs like so:

    public void changeVolume(float newVolume)
    {
    PlayerPrefs.SetFloat("volume", newVolume);
    AudioListener.volume = PlayerPrefs.GetFloat("volume");
    }


    And then adding the line "AudioListener.volume = PlayerPrefs.GetFloat("volume");" on Start on the camera with the audo listener and hooking up the slider but that doesn't do anything.

    Does anyone know how to make this work with a UI slider? It has to be the audiolistener that changes volume not the audio sources. Thank you in advance
     
  2. rubcc95

    rubcc95

    Joined:
    Dec 27, 2019
    Posts:
    222
    I don't know where u found this codes but... why just not this?
    Code (CSharp):
    1. public void ChangeVol(float newValue)
    2. {
    3.     AudioListener.volume = newValue;
    4. }
    and this:
    Code (CSharp):
    1. public void changeVolume(float newVolume)
    2. {
    3.     PlayerPrefs.SetFloat("volume", newVolume);
    4.     AudioListener.volume = newVolume;
    5. }
    If you want to do this with an slider, use onValueChanged event, remember to just record it at PlayerPrefs only when you left the menu, instead of each frame the slider is moving for optimization purposes:

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3.  
    4. public class VolumeController : MonoBehaviour
    5. {
    6.     [SerializeField] Slider slider;
    7.  
    8.     void Awake()
    9.     {
    10.         AudioListener.volume = PlayerPrefs.GetFloat("volume");
    11.         slider.onValueChanged.AddListener(delegate { ChangeVolume(); });
    12.     }
    13.  
    14.     void ChangeVolume() => AudioListener.volume = slider.value;
    15.  
    16.     //Called from the volume menu button for example
    17.     public void OnClick()
    18.     {
    19.         if (PlayerPrefs.GetFloat("volume") != AudioListener.volume) PlayerPrefs.SetFloat("volume", AudioListener.volume);
    20.     }
    21. }
    22.  
     
    Jeepster likes this.
  3. Jeepster

    Jeepster

    Joined:
    Jan 23, 2014
    Posts:
    401
    You're a lifesaver! Thank you!! It works perfectly with the slider and I like the optimization aspect. Have a great day