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. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

Audio Audio Mixer and Slider

Discussion in 'Audio & Video' started by affinixy, Sep 19, 2018.

  1. affinixy

    affinixy

    Joined:
    Nov 2, 2017
    Posts:
    13
    Hi guys,

    I am using Unity 2018.2.6f1
    Just want to check if this is a problem with my Unity version. Because in the video tutorial, it was working fine.

    I was following this tutorial to create a slider for my sound and music.


    The music and sound volume is saved correctly to my playerpref, and i am able to load the value and apply back to the slider that is controlling the exposed Audio mixer group.

    But for some reason, the even when the slider is loaded with the playerpref value, the audio mixer is still not showing the loaded value.
    https://www.dropbox.com/s/omk111ml23t5vz5/audio.jpg?dl=0

    Can anyone tell me what is wrong with my code?
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.Audio;
    3. using UnityEngine.UI;
    4.  
    5. public class MixerController : MonoBehaviour {
    6.  
    7.     public AudioMixer audioMixer;
    8.     [Space(10)]
    9.     public Slider musicSlider;
    10.     public Slider sfxSlider;
    11.  
    12.  
    13.     public void SetMusicVolume(float volume)
    14.     {
    15.         audioMixer.SetFloat("musicVolume", volume);
    16.     }
    17.     public void SetSfxVolume (float volume)
    18.     {
    19.         audioMixer.SetFloat("sfxVolume", volume);
    20.     }
    21.     private void OnEnable()
    22.     {
    23.         //load playerpref and apply to slider UI
    24.         musicSlider.value = PlayerPrefs.GetFloat("musicVolume", 0);
    25.         sfxSlider.value = PlayerPrefs.GetFloat("sfxVolume", 0);
    26.  
    27.         //load playerpref for music and sfx volume
    28.         //SetMusicVolume(PlayerPrefs.GetFloat("musicVolume", 0));
    29.         //SetSfxVolume(PlayerPrefs.GetFloat("sfxVolume", 0));
    30.  
    31.     }
    32.  
    33.     private void OnDisable()
    34.     {
    35.         float musicVolume = 0;
    36.         float sfxVolume = 0;
    37.  
    38.         audioMixer.GetFloat("musicVolume", out musicVolume);
    39.         audioMixer.GetFloat("sfxVolume", out sfxVolume);
    40.  
    41.         PlayerPrefs.SetFloat("musicVolume", musicVolume);
    42.         PlayerPrefs.SetFloat("sfxVolume", sfxVolume);
    43.         PlayerPrefs.Save();
    44.     }
    45.  
    46. }
    47.  
     
  2. affinixy

    affinixy

    Joined:
    Nov 2, 2017
    Posts:
    13
    Sorry, my bad.
    I should have just add a script to camera to change the value of the audio mixer
    Code (CSharp):
    1. public AudioMixer audioMixer;
    2.  
    3.     // Use this for initialization
    4.     void Start () {
    5.         //load from sound music slider
    6.         audioMixer.SetFloat("musicVolume", PlayerPrefs.GetFloat("musicVolume", 0));
    7.         audioMixer.SetFloat("sfxVolume", PlayerPrefs.GetFloat("sfxVolume", 0));
    8.     }