Forum rules - please read before posting.

Animator Audio Sources Spawning in Scene - Not Recognized By AC Sound

Unity: 2019.4.18f1
AC: 1.74.2


Hello. I've attached several sound effects to a Behaviour element in an animator. The animator calls an audio source from the project folder, and spawns/clones it in the scene. It works with an audio mixer, but it doesn't work with the AC sound component.

I'd use the audio mixer, but I had to revert to the AC sound component because of mixer duplicates after spawning (Addressables).

Maybe AC sounds aren't recognized because the prefab is pulled from the project folder, and not initially in the scene. The animator isn't allowing me to pull prefabs from the scene.

Is there a workaround for this, or a way to force the spawned/cloned prefab to use the original audio mixer or AC sound component, after spawning?

Comments

  • edited January 2022

    Duplicate mixer groups due to Addressables is an issue I've encountered before, and isn't unique to AC projects.

    I'm not clear on the way it doesn't work with the AC Sound component, though. You're referring to a prefab that has a Sound object set up in such a way that would work normally if present in the scene file?

    If you're using Audio Mixers, volume is controlled via them - so you could bypass the Sound component so long as the Mixer asset isn't a duplicate.

    It may be worth trying to have an AC Sound object in the scene, and grab it's mixer at the time the Animator spawns in the object. Something like:

    GameObject sceneObject = GameObject.Find ("MySound");
    var mixer = sceneObject.GetComponent<AudioSource>().outputAudioMixerGroup;
    spawnedAudioSource.outputAudioMixerGroup = mixer;
    
  • edited January 2022

    Yes. Normally, if I added the prefab directly to the scene with the AC sound component, it would work. But it doesn't work because it's pulled in from a project folder (from a footstep for example).

    Upon loading a scene, if there is an audio mixer already on an object in the scene, or even an audio mixer being called in an AC Call Event ActionList/Cutscene, that mixer becomes a duplicate, automatically, at runtime. Grabbing a mixer from the scene wouldn't work. Is there a way to grab a mixer from a prefab from within a folder instead?

    (Not a coder) Also, is this correct? I'm getting this error:
    (12,9): error CS0103: The name 'spawnedAudioSource' does not exist in the current context.
    This is (12,9): spawnedAudioSource.outputAudioMixerGroup = mixer;


    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.Audio;
    
    public class ACSoundChange : MonoBehaviour
    {    
        void Awake() 
        {
            GameObject sceneObject = GameObject.Find ("MySound");
            var mixer = sceneObject.GetComponent<AudioSource>().outputAudioMixerGroup;
            spawnedAudioSource.outputAudioMixerGroup = mixer;
        }
    }
    

    For the script below, currently, an audio mixer can simply be dragged in. But, can it be modified to call on a specific asset's name instead (the audio mixer)? Or, maybe call on the mixer addressable?


    using UnityEngine;
    using UnityEngine.Audio;
    
    public class ACAddMixerTEST : MonoBehaviour
    {
        public AudioSource audioSource;
        public AudioMixerGroup audioMixerGroup;
    
        void Awake() 
        {
            audioSource.outputAudioMixerGroup = audioMixerGroup;
        }
    }
    
  • edited January 2022

    it doesn't work because it's pulled in from a project folder

    In what way does it not work, though? Does audio not play, or is the volume wrong?

    (Not a coder) Also, is this correct? I'm getting this error:

    You mentioned earlier you were spawning a prefab into the scene, so I was presuming you had a separate script to handle this - the code snippet I shared was an example to incorporate into that script.

    But, can it be modified to call on a specific asset's name instead (the audio mixer)? Or, maybe call on the mixer addressable?

    You can replace AudioMixerGroup with AssetReference if you want to load it by reference, but AC isn't involved here. Unity's docs have code examples for working with Addressables through script, however.

  • edited January 2022

    Solution: The information below are tips that may, or may not be necessary. This is what works for me. This has been tested on Steam, using Addressables. This works for spawned sound effects, Addressables, non-Addressables and audio sources located inside an Addressables scene.

    • Removed all references to the Audio Mixer from everything, including AC Settings Manager. Reverted to 'Audio Sources' in AC Settings Manager. (If the Audio Source is a prefab, make sure the Audio Mixer slot is clear/empty/reverted to original. Otherwise, the script won't replace it.)

    • All Scenes are Addressable, except for a non-AC boot-up scene, and a non-AC loading scene. (There is no Game Engine in those 2 scenes. They are the only 2 scenes in the Player Build Settings. The Addressable name for the non-AC loading scene is added to AC Settings Manager.)

    • Added the Audio Mixer to a 'Resources' folder.

    • Add the below script to an object with an audio source, as a component. Duplicate the script for each Audio Mixer Group. (1 for SFX, 1 for System Sounds, 1 for Music...Etc.)

    • If your Audio Mixer Group (Master) has groups beneath it, a / needs to separate the groups. If it's just SoundEffects, then just add SoundEffects.)


    using UnityEngine;
    using UnityEngine.Audio;
    
    public class MixSoundEffects : MonoBehaviour
    
    {
        void Start()
        {
            //Get the AudioSource
            AudioSource audioSource = GetComponent<AudioSource>();
    
            //Load AudioMixer
            AudioMixer audioMixer = Resources.Load<AudioMixer>("Audio Mixer Name Goes Here");
    
            //Find AudioMixerGroup you want to load
            AudioMixerGroup[] audioMixGroup = audioMixer.FindMatchingGroups("Master/SoundEffects Name Goes Here");
    
            //Assign the AudioMixerGroup to AudioSource (Use first index)
            audioSource.outputAudioMixerGroup = audioMixGroup[0];
        }
    }
    
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Welcome to the official forum for Adventure Creator.