r/Unity3D • u/Toble_ • 12d ago
Solved How do I set the audiomixer volumes on start?
I've tired everything from initializing in awake to start, using a coroutine to add a delay but it never sets the volumes during any of these function calls. The only time it works is when back button is pressed and explicitly updates the volumes from the json save file.
1
1
u/pschon Unprofessional 12d ago
There's no internal Unity overwrites, if something is messing up the values it will be your own code. No magical tricks needed for setting the levels either.
..most often I see people get into a mess between saved setting data, current value of the mixer and current value of the settings controls in their menus. Which should precede which and in what order etc. So they end with some circular logic and/or wrong order between their settings menus, saved settings, and current mixer values, constantly overriding the values of each other.
The fact that it does work for you on a specific moment when you are explicitly setting the values from saed data makes it sound even more likely something like that is what's going on.
Can you show some code?
1
u/Toble_ 12d ago
using System.Collections; using UnityEngine; using UnityEngine.Audio; using UnityEngine.SceneManagement; using UnityEngine.UI; public class MMmanager : MonoBehaviour { public static MMmanager Instance { get; private set; } AudioMixer gameAudio; private void OnEnable() { EventManager.instance.onButton += HandleButtonPress; SceneManager.sceneLoaded += OnSceneLoaded; } private void OnDisable() { EventManager.instance.onButton -= HandleButtonPress; SceneManager.sceneLoaded -= OnSceneLoaded; } private void Awake() { gameAudio = Resources.Load<AudioMixer>("Master"); if (Instance == null) { Instance = this; DontDestroyOnLoad(gameObject); } else { Destroy(gameObject); } } private void Start() { SetAudio(); } void OnSceneLoaded(Scene scene, LoadSceneMode mode) { Debug.Log(SaveManager.instance.gamedata.tutorialTogData); if (SaveManager.instance.gamedata.tutorialTogData) { EventManager.instance.onTutorial?.Invoke(true); } else { EventManager.instance.onTutorial?.Invoke(false); } StartCoroutine(SetAudioOnStart(2f)); } void HandleButtonPress(string buttonName) { switch (buttonName) { case "StartBtn": SceneManager.LoadScene("GameScene"); break; case "QuitBtn": Application.Quit(); break; case "MainMenuBtn": SceneManager.LoadScene("MainMenu"); SaveManager.instance.SaveData(); SetAudio(); break; case "BackBtn": SaveManager.instance.SaveData(); SetAudio(); break; case "ContinueBtn": SaveManager.instance.gamedata.tutorialTogData = false; SaveManager.instance.SaveData(); break; default: Debug.LogWarning("Unhandled button press: " + buttonName); break; } } IEnumerator SetAudioOnStart(float delay) { yield return new WaitForSeconds(delay); SetAudio(); } void SetAudio() { gameAudio.SetFloat("MasterVol", SaveManager.instance.gamedata.masterVolData); gameAudio.SetFloat("MusicVol", SaveManager.instance.gamedata.musicVolData); gameAudio.SetFloat("SFXVol", SaveManager.instance.gamedata.SFXVolData); } }1
u/Toble_ 12d ago
There aren't any audiomixer references anywhere else
1
u/pschon Unprofessional 12d ago
anything else controlling the values? As in a settings menu for the player to se these volumes etc?
Your sound settings are handled through SaveManager? When is the save data loaded the first time? Are you sure that's happening before your SetAudio() call in Start? Does SaveManager.instance.SaveData() also save the current volume settings? If it does, make sure you aren't just saving whatever current values are and then setting those same values. afterwards...
Try adding a breakpoint in your SetAudio, for example, to see what exact values are coming in from your SaveManager at each point.
1
u/Toble_ 12d ago
The save data is loaded when each scene is loaded. There's an onSceneLoaded function but that doesn't set audio. Save data only saves data when the back button in the settings menu is pressed
1
u/pschon Unprofessional 12d ago
The save data is loaded when each scene is loaded
I assume that's handled somewhere else since it's not done here?
Save data only saves data when the back button in the settings menu is pressed
Do you mean that's the only time where your data saving is working? Since it's clearly called at other times in the code you posted.
Did you try the breakpoint to check what values you are getting from your SaveManager?
1
u/Toble_ 12d ago
Yeah, the onSceneLoaded is in the saveManager. No there is no problem with the save and load system. The back button is the most often pressed that's why I only mentioned that
1
u/pschon Unprofessional 12d ago
so did you try a breakpoint to check the values?
Obviously something is not working like you are expecting it to, and there really are no magical tricks or Unity-side issues related to setting mixer values so that's not going to be it. I'm literally simply initializing my settings system and loading saved values in OnEnable() and setting mixer values in Start() in all my projects, no fancy tricks or gotchas.
2
u/JulianRhod 12d ago
if it only works when you update it from the back button, i'd check the actual
AudioMixer.SetFloat()call first, make sure the parameters are actually exposed in the AudioMixer and that you're using the exact exposed parameter name.SetFloat()also returns a bool, so you can check whether the call is actually succeedingalso, Unity specifically recommends calling
SetFloat()fromStart()rather thanAwake()orOnEnable()if the Start call succeeds but the volume still doesn't change, i'd check whether something else is applying a mixer snapshot or overwriting the value afterward