The only other answer I’ve found to a similar problem is to add the mixer as a public variable. But this can’t be done when using PlayClipAtPoint().
I’ve tried to write my own public static method that does what PlayClipAtPoint does, but I can’t change the audio mixer group in the script after adding the audiosource component.
I don’t know why you can’t change the outputAudioMixerGroup after adding the audio source. Also, PlayClipAtPoint() also uses default values for all AudioSource parameters for the temporary objects it makes, so if you want some specific behaviour, PlayClipAtPoint() is not very useful.
But with your replacement method you can do this:
Create a set of prefabs of gameobjects that have an AudioSource component
In the prefabs you can set different mixer groups and other AudioSource settings
In you replacement method instantiate one of these prefabs as your temporary audio source depending on situation. Something like:
Late as well, but I also have a different workaround which I found to be very simple. The 3rd parameter of PlayClipAtPoint() controls the volume, and you can get the volume from the Audio Mixer.
Attach an audio source to the object calling PlayClipAtPoint(), link the mixer to the output, and add the following code in. Tailor it to your specific case.
private AudioMixerGroup audioMixerGroup;
void Start()
{
audioMixerGroup = GetComponent<AudioSource>().outputAudioMixerGroup;
}
private void PlaySound()
{
bool value = audioMixerGroup.audioMixer.GetFloat("Sound", out float volume);
volume = Mathf.Pow(10f, volume / 20); //converted from Log10
AudioSource.PlayClipAtPoint(clip, Camera.main.transform.position, volume);
},Late as well, but I have a different workaround which I found to be very simple. The 3rd parameter of PlayClipAtPoint() controls the volume, and you can get the volume from the Audio Mixer.
Just attach an audio source to the object calling PlayClipAtPoint(), link the mixer to the output, and add the following code in. Tailor it to your specific case.