Search Unity

Question How To Set AudioSource outputAudioMixer parameter at start

Discussion in 'Scripting' started by stfunity, Jul 14, 2021.

  1. stfunity

    stfunity

    Joined:
    Sep 9, 2018
    Posts:
    65
    Nearly done with my quest to fix amnesiac prefabs at runtime.. ran into a weird one where a bunch of child object audio sources have no mixer routing. The place from which the noise emanates matters since it's not UI stuff.

    The general <AudioMixer> is called "SoundtrackAudioMixer" in our project files.
    We have a top level <AudioMixerGroup> called "Master", under which there are a bunch of other <AudioMixerGroup>s. The one I want is named "Foley"

    How do I get to it? Tried making a public reference up top like so
    Code (CSharp):
    1.     public AudioSource emitterAudioSource;
    2.     public AudioClip emitterFiringSound;
    3.     [SerializeField]
    4.     public AudioMixerGroup foleyMixer;
    And I even went and set the script import defaults to that specific "Foley" <AudioMixerGroup> but I .. suspect that since that new variable and the import default I assigned to it are newer than the prefab in question.. it doesn't count? Ex post facto file management? I am kinda confused about that but it's neither here nor there.

    Either way, I'm not seeing a passthrough yet --- the code below DOES pass a clip to the right <AudioSource>, so I am touching the component I want but it won't pass that ouputAudioMixerGroup.

    Code (CSharp):
    1.             string mixerName = "SoundtrackAudioMixer";
    2.             AudioMixer mixer = Resources.Load<AudioMixer>(mixerName);
    3.             string foleyName = "Foley";
    4.             AudioMixerGroup foley = Resources.Load<AudioMixerGroup>(foleyName);
    5.             emitterAudioSource = gameObject.GetComponent<AudioSource>();
    6.             emitterAudioSource.clip = emitterFiringSound;
    7.             emitterAudioSource.outputAudioMixerGroup = foley;
    I saw another thread mentioning

    Code (CSharp):
    1. Resources.Load("AudioMixer/AudioMixerGroup") as AudioMixerGroup;
    Am I just getting the path syntax wrong? Should I be saying "SoundtrackAudioSource/Master/Foley"? What is the deaaaaaalllllllllll.

    Thanks!