- Home /
Setting a specific Audio Mixer Group through code
Hey there, I'm trying to create a settings menu where I can adjust the volume of different audio groups but with the way I've made the audio manager I can't figure out a way to create the audio sources with the audio groups attached to them. I'm creating the audio Sources as the scene starts and the audio is played once I trigger something like a jump or when I shoot. But none of the audio Sources have an audio mixer group on them. If anyone could help me out that would be great. The code for the Audio Manager:
public Sound[] sounds;
// Use this for initialization
void Awake () {
foreach (Sound s in sounds)
{
s.source = gameObject.AddComponent<AudioSource>();
s.source.clip = s.clip;
s.source.volume = s.volume;
s.source.pitch = s.pitch;
}
}
public void Play (string name)
{
Sound s = Array.Find(sounds, sound => sound.name == name);
s.source.Play();
}
The code for The Sound Script:
public string name;
public AudioClip clip;
[Range(0f, 1f)]
public float volume;
[Range(.1f, 3f)]
public float pitch;
[HideInInspector]
public AudioSource source;
I hope that made sense ( I'm relatively new to implementing audio ) Thank you for your time.
Answer by MathewJProductions · Feb 20, 2019 at 10:05 PM
For anyone that sees this, I have kind of got it working. I changed the Audio Manager to this:
public Sound[] sounds;
public AudioMixer mixer = Resources.Load("GameAudio") as AudioMixer;
// Use this for initialization
void Awake () {
foreach (Sound s in sounds)
{
s.source = gameObject.AddComponent<AudioSource>();
s.source.clip = s.clip;
s.source.volume = s.volume;
s.source.pitch = s.pitch;
string OutputMixer = "SoundEffects";
GetComponent<AudioSource>().outputAudioMixerGroup = mixer.FindMatchingGroups(OutputMixer)[0];
}
}
public void Play (string name)
{
Sound s = Array.Find(sounds, sound => sound.name == name);
s.source.Play();
}
This works but only on the first Audio Source ( i have 8 different ones ). If anyone could help me out with this that would be great, thanks.
Hey, I recognize this code. It's from the Brackey's tutorial, right? I'm having the same problem. I want different sounds to be controlled by different audio$$anonymous$$ixerGroups. I tried putting "public Audio$$anonymous$$ixerGroup audio$$anonymous$$ixerGroup;" at the beginning of Sound.cs, but it did not work. I have a question regarding the above code. What is the purpose of "[0]" at the end of line 16?
Answer by Ben_Grange · Jan 27, 2021 at 07:21 PM
@MathewJProductions and @MerlinsMaster — I realize I'm a little late to this post, but if anyone is still looking for help on this:
You've serialized the audio mixer in the AudioManager class. You need to serialize the audio mixer group in the Sound class. Then, once you've done that, you need to iterate the audio mixer group through the array in your AudioManager class. So, it would look like this:
Audio Manager:
foreach(Sound s in sounds)
{
s.source = gameObject.AddComponent<AudioSource>();
s.source.clip = s.clip;
s.source.outputAudioMixerGroup = s.audioMixerGroup;
s.source.volume = s.volume;
s.source.pitch = s.pitch;
}
And in the Sound class:
public class Sound
{
public string name;
public AudioClip clip;
public AudioMixerGroup audioMixerGroup;
[Range(0f, 1f)] public float volume;
[Range(.1f, 3f)] public float pitch;
[HideInInspector] public AudioSource source;
}
Then you can assign the specific audio mixer group in the inspector. Hope this helps!