Attach Mixer to Audio Source via script

Hi!

Would like to ask how i could possibly have the mixer i made link to the audio source via script, i followed Brackeys tutorial on how to create a custom audio manager in unity(link for reference

) that can be used to call any sound on any part of the game , so far it works fine but when i tried to link it to the slider i made on the settings area(tutorial also by Brackeys (link for reference:

)

, i realized that on the audio source i have to point it to the master mixer so that the slider would work , apparently since the component is called after i click play theres no way for me to attach the mixer manually and have to do it by code . anyway heres my code below / Brackeys Code below. Apologies for the comments , im a beginner programmer and wanted to write notes everywhere so i could learn them better.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
// with this we will be able to use array.find to look for our indicated sound
//ugh puking inang Case sensitive yan MotherBleep
//Trying something else, puking ina
using UnityEngine.Audio;
//new function that has all Unity package wraped up in one
//goal is to have loop function and audio call / source and play method
//create a custom class for sound

public class SoundGuy : MonoBehaviour {
// Use this for initialization
public SoundGuyCC[ ] sounds;
// [ ] ← array

void Awake()
{
//same as the start method but is called right before
// goal is to loop onto a list
foreach (SoundGuyCC s in sounds)//loop to the list array
{
s.source = gameObject.AddComponent();

//component audiosource should be hosted in a variable so it can be easily called
//added s.source to point it to the wright source
s.source.clip = s.clip;
s.source.volume = s.volume;
s.source.pitch = s.pitch;

}
}
void Start()
{
Play(“x”);
}
//play stuff method
public void Play(string name)
{
AudioMixer mixer = Resources.Load(“MasterMixer”) as AudioMixer;
SoundGuyCC s = Array.Find(sounds, sound => sound.name == name);

s.source.Play();
//where —> =>

}

}

heres the other public SoundGuyCC code

using UnityEngine.Audio;
using UnityEngine;
//this is a custom Class
[System.Serializable]
// for this to appear in the inspector need to make it serializable
public class SoundGuyCC
{
//not using monobehaviour
public string name;
public AudioClip clip;
[Range(0f, 1f)]
//slider range volume
public float volume;
[Range(.1f, 3f)]
//slider range volume
public float pitch;

[HideInInspector]//need to hide this as the source will be looped depending on
//which variable is called , even though public itll be hidden since it changes.
public AudioSource source;
}

Many Thanks!

Figured this out!

only changed my main code .

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
// with this we will be able to use array.find to look for our indicated sound
//ugh puking inang Case sensitive yan MOTHERFUCKER!
//Trying something else, puking ina
using UnityEngine.Audio;
//new function that has all Unity package wraped up in one
//goal is to have loop function and audio call / source and play method
//create a custome class for sound
public class SoundGuy : MonoBehaviour {
// Use this for initialization
//added this here to map the custom Mixer

public AudioMixerGroup audioMixer;<-- Added this here

public SoundGuyCC[ ] sounds;
// [ ] ← array

void Awake()
{
//same as the start method but is called right before
// goal is to loop onto a list
foreach (SoundGuyCC s in sounds)//loop to the list array
{
s.source = gameObject.AddComponent();

//component audiosource should be hosted in a variable so it can be easily called
//added s.source to point it to the wright source
s.source.clip = s.clip;
s.source.volume = s.volume;
s.source.pitch = s.pitch;

//added this here to map it to have the custom mixer
s.source.outputAudioMixerGroup = audioMixer; ← added this here

}
}
void Start()
{
Play(“x”);
}
//play stuff method
public void Play(string name)
{

SoundGuyCC s = Array.Find(sounds, sound => sound.name == name);

s.source.Play();
//where —> =>

}

}

9 Likes

Thank You so much! Though it was a bit confusing this worked for me, been looking for the solution for a while now!

Thx it worked for me to after i selected new option “Audio Mixer” to Master group from updated AudioManager

Top Solution! i followed also Brackeys. These two Lines are the Solution:
public AudioMixerGroup audioMixer; //Make a Slot to put your audioMixer inside
s.source.outputAudioMixerGroup = audioMixer; //Add your choosen audioMixer to every sound. so that you can manipulate the volume via your audioMixer
And dont forget to put your mixer inside of your AudioManager Object! Otherwise it wont work

1 Like

YOU SIR ARE MY LORD AND SAVIOUR

I have spent the entire day and have finally found this! too op

this is amazing, thank you!

Haha the code comments was a mess to read, but the solution was ok =) thnx

Thank you! after i changed Public AudioMixer to AudioMixerGroup , it’s working.