Change Audio Settings of spawned opjects by ObjectPool/SurfaceManager

MarkusKer

Member
I am using the audiomixer groups to change the volume of the game. With normal audio sources it is easy to attach the audio mixer desired, However i cannot figure out where the audio source component of for example the bullet impact is located or if there is such a component at all?
As far as i can tell during runtime a audiosource gets created under the "Game" Object. I would create a script which is changing the mixer at startup but i was wondering if there is a simpler solution?
 

nathanj

Member
This is one of my biggest issues with UCC as well. For example, the only way you can easily modify the volume of sounds (say punching) is to edit them externally.

Something like instantiating an Audio Source for specific types of sounds would be a HUGE help. At the moment, all sounds enherit the same settings as the audio source that preceeded it (on the game object it is created on).
 

MarkusKer

Member
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Audio;
public class attachAudiotoMaster : MonoBehaviour
{
public AudioMixerGroup AudioMixer;
public AudioSource[] Audiosources;
public bool includeChildren;
// Start is called before the first frame update
void Start()
{
if (includeChildren == false)
{
Audiosources = GetComponents<AudioSource>();
if (Audiosources.Length != 0)
{
for (int i = 0; i < Audiosources.Length; i++)
{
Audiosources.GetComponent<AudioSource>().outputAudioMixerGroup = AudioMixer;
}
}
else
{
Debug.Log("NoAudioSourceAttached " + gameObject.name);
}
}
if (includeChildren == true)
{
Audiosources = GetComponentsInChildren<AudioSource>();
if (Audiosources.Length != 0)
{
for (int i = 0; i < Audiosources.Length; i++)
{
Audiosources.GetComponent<AudioSource>().outputAudioMixerGroup = AudioMixer;
}
}
else
{
Debug.Log("NoAudioSourceAttachedinComponents " + gameObject.name);
}
}
}
}
 

nathanj

Member
Thanks for sharing this, but it seems to still have the same issue in that it assigns all the audio sources to the same audio channel?

So for example, if you have it search for children then your footstep audio will go through the same audio channel as your punch sounds. In my case, I have a script that listens for if the player’s height has changed and if they are crouching their footstep sound is decreased. I wouldn’t want all my players sounds decreased in this instance. It’s just an example, but having more control from the start would allow for making a much more nuanced experience for the user.
 

MarkusKer

Member
I see, i solved this problem the following way. I created a script attached to the player itself which is listening for changes if a ability state changes.
 

MarkusKer

Member
void Awake()
{
///listen for abilities (in this case the player, this script is attached on the player root!
EventHandler.RegisterEvent<Ability, bool>(gameObject, "OnCharacterAbilityActive", SpeedChangeActive);

}
private void SpeedChangeActive(Ability ability, bool activated)
{///you can than check which state is active(Note that you must add a unique state name under the ability)
if(ability.State == "Run")
{
if (activated == false)///or true
{
DO Stuff(for example play another game objects audio source once)
}
}

}
 

nathanj

Member
That’s a good way, atm i’m using the state system to switch the audio volume. It works well but my point is more that having the ability to assign multiple audio sources on the player to different audio channels would be extremely useful.

Thanks for posting your code here, I’m sure many other will find it useful.

Nathan
 

Andrew

Moderator
Staff member
Glad to see you're finding ways to extend the controller to suit your needs :)
 

rostom

New member
Glad to see you're finding ways to extend the controller to suit your needs :)
Hello, not everybody is finding there way around! cause I'm not a programmer, i use Playmaker for developing my project. and i need help to how could i set the output of the AudioSources that are created under the "Game" Object to an Audio Mixer Group?
 

rostom

New member
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Audio;
public class attachAudiotoMaster : MonoBehaviour
{
public AudioMixerGroup AudioMixer;
public AudioSource[] Audiosources;
public bool includeChildren;
// Start is called before the first frame update
void Start()
{
if (includeChildren == false)
{
Audiosources = GetComponents<AudioSource>();
if (Audiosources.Length != 0)
{
for (int i = 0; i < Audiosources.Length; i++)
{
Audiosources.GetComponent<AudioSource>().outputAudioMixerGroup = AudioMixer;
}
}
else
{
Debug.Log("NoAudioSourceAttached " + gameObject.name);
}
}
if (includeChildren == true)
{
Audiosources = GetComponentsInChildren<AudioSource>();
if (Audiosources.Length != 0)
{
for (int i = 0; i < Audiosources.Length; i++)
{
Audiosources.GetComponent<AudioSource>().outputAudioMixerGroup = AudioMixer;
}
}
else
{
Debug.Log("NoAudioSourceAttachedinComponents " + gameObject.name);
}
}
}
}
i just tried this script but it is only getting the Audiosources on the "Game" object, it is not setting there output to the AudioMixer.
 

nathanj

Member
Looks like there is a public field you need to set:
Code:
public AudioMixerGroup AudioMixer;

If you look at the script in the Inspector, can you drag an audio mixer channel into that field?
 

nathanj

Member
I just copied that script into my project but its throwing some errors and i'm not sure it will work even with that fixed. It's not adding the audio sources to the array in the first place.

I'm just about to go to bed and could be readin this wrong with my sleepy brain, and if that is the case i aplogies. I'll try to have a look at this tomorrow and if i figure it out i'll post a fix sometime then.

Edit 1. Ah, I see. The audio sources are supposed to be added manually, through the editor. This is ok, but a dynamically populated list would be more ideal for collecting instantiated audio sources

Edit 2. Just tried a simple modification but I can't understand the use of a public array here, If you already have access to the Audio Sources why not just change their mixer? Or, does this mixer channel get set in runtime and that IS the point of this, to set it back? I can't remember atm.

If that's the case and we just want to reassign the mier channel that gets reset in runtime, try this, add all your audio sources you want to set to a specific audio channel in the mixer. Note, this is bog standard and just for testing purposes.

Code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Audio;
public class SetAudioMixer : MonoBehaviour
{
    public AudioMixerGroup audioMixer;
    public AudioSource[] audiosources;
    // Start is called before the first frame update
    void Start()
    {

        if (audiosources != null)
        {
            foreach (AudioSource audioSource in audiosources)
            {
                audioSource.outputAudioMixerGroup = audioMixer;
            }
        }
        else
        {
            Debug.Log("NoAudioSourceAttached");
        }
    }
}
 
Last edited:

rostom

New member
Looks like there is a public field you need to set:
Code:
public AudioMixerGroup AudioMixer;

If you look at the script in the Inspector, can you drag an audio mixer channel into that field?
i cannot drag and drop it but i sure did select it and assigned it.
 

rostom

New member
I just copied that script into my project but its throwing some errors and i'm not sure it will work even with that fixed. It's not adding the audio sources to the array in the first place.
instead of: "Audiosources.GetComponent<AudioSource>().outputAudioMixerGroup = AudioMixer;" you just type: "GetComponent<AudioSource>().outputAudioMixerGroup = AudioMixer;" in both line 20 and 35, that should fix the errors issue. and to add the Audiosources to the array i just ticked the "include children" checkbox. The script is getting the audiosources but it is not setting up there output
 

rostom

New member
ok :), other then the character falling sound that is still playing even if the AudioMixer Volume is turned down to 0, i found my way around for using audioMixers and UCC with Playmaker FSM attached to the "Game" object, and since the other AudioSources will Copy from the first one that spawns at run time, i just get the first AudioSource, get the output property and set it to the AudioMixer of my choice.
i still recommend UCC team to take a look at AudioMixers and how they operate, cause they are real time and effort savers. and of curse make it a bit easier to implement them with the AudioSources and the AudioManager.
Thank you, have a good day:)
 

nathanj

Member
I completely agree that more control from the Audio Manager would be extremely helpful.

Also, with that above script, for checking children changing GetComponent to GetComponentsInChildren should work
 
Top