I’m trying to load some settings I saved the last time I played and set them right on Awake but it seems like AudioMixer’s SetFloat doesn’t work there…
Mmm would be good if this was at least noted in the docs or something. Ran into this issue and spent an hour trying to debug it, only to find that it was a unity bug.
Same - just filed (yet another) bug report to that effect. I can’t see why SetFloat would work from Start(), but NOT from Awake(). If it is intended behaviour then it would be nice to receive a warning, or a mention of it in the documentation.
On a related note, it would also be nice if it was mentioned somewhere that the second parameter of SetFloat is meant to be in decibels, and not as logic would dictate (0 for “off” and “1” for full loud).
Sometimes, I wish the Unity documentation had a face, then I’d have something to punch over oddities like this.
Just to report that it doesn’t work when set in OnEnable() either (which executes after Awake and before Start).
My guess is that the internal works of the AudioMixer is resetting all attenuation to 0dB in it’s own OnEnable() method. I will add a request for the AudioMixer documentation to clarify.
It’s wierd, i wrote a sound management script a year ago and the initialization of the float parameters for the audio mixer were called on the Awake() method, all worked well, i even tested the old project i wrote the script for, still worked… now i am working on a new project and used that same script and got this problem, only works with the Start() method.
Hmm, August 2019 and this is still causing issues. I found this thread trying to figure out why my Ambient sounds were always at some blasting volume even when I set them in Awake().
From looking at the bug that @deeprest opened, apparently setting AudioMixer parameters in Awake is undefined behavior. The parameters must be set in Start. Weird…but there you have it.
Won’t it be possible to create a method to set the defalut values of the AudioMixer that could run from Awake?
My entire framework initialises every system that can be configurable via loading a save file in Awake, all the input options, graphics options, language, accessibility - everything, but with audio I have to run a coroutine that runs after 0.01 seconds after the Awake in order to load the values from the save file. It’s kinda hacky.
Also, if it does work in builds, is it a defined behaviour? Like, can I make different methods to run if the game runs in editor and in the build, and it it’s the build - will the changing of the values work in Awake flawlessly?
If you load the settings file synchronously in Awake(), can you not apply the values in Start()? Start is called just before the first Update().
I assume not. You must determine if it’s worth your time to rely on “undefined behaviour”. If it seems to work, then great, but it might not work for everyone/all platforms the same, so you might need to put time into fixing it later anyway.
For the few audio settings I have, I handle them explicitly in Start(). Before this thread, I did something like the code below. We now know the #if directive should not be necessary, and is probably a bad idea.
void Start()
{
#if UNITY_EDITOR
// workaround for Unity Editor bug where AudioMixer.SetFloat() does not work in Awake()
mixer.SetFloat( "MasterVolume", Util.DbFromNormalizedVolume( FloatSetting["MasterVolume"].Value ) );
mixer.SetFloat( "MusicVolume", Util.DbFromNormalizedVolume( FloatSetting["MusicVolume"].Value ) );
mixer.SetFloat( "SFXVolume", Util.DbFromNormalizedVolume( FloatSetting["SFXVolume"].Value ) );
#endif
}
I handle all settings generically in Awake(), then apply audio settings like this in Start().
Ugh. I have different behaviour on two different scripts in the same project. In one script calling SetFloat(…) from Awake() is working fine, in another it’s doing nothing.
It should be noted that the statements in the bug response and the docs page for AudioMixer.SetFloat(…) directly contradict each other. I gave feedback to that effect on the docs page, about to bug report it, too.