Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Attach Mixer to Audio Source via script

Discussion in 'Scripting' started by hail_miki, Jul 17, 2018.

  1. hail_miki

    hail_miki

    Joined:
    Jul 17, 2018
    Posts:
    40
    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 <s> array
    {
    s.source = gameObject.AddComponent<AudioSource>();

    //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!
     
  2. hail_miki

    hail_miki

    Joined:
    Jul 17, 2018
    Posts:
    40
    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 <s> array
    {
    s.source = gameObject.AddComponent<AudioSource>();

    //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 ---> =>

    }

    }
     
  3. phantom_x404

    phantom_x404

    Joined:
    Apr 26, 2020
    Posts:
    26
    Thank You so much! Though it was a bit confusing this worked for me, been looking for the solution for a while now!
     
  4. PDaniek

    PDaniek

    Joined:
    Mar 30, 2020
    Posts:
    1
    Thx it worked for me to after i selected new option "Audio Mixer" to Master group from updated AudioManager
     
  5. Ollibanjo

    Ollibanjo

    Joined:
    Feb 5, 2020
    Posts:
    2
    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
     
    Soran5 likes this.
  6. ThyBotz

    ThyBotz

    Joined:
    Aug 31, 2020
    Posts:
    1
    YOU SIR ARE MY LORD AND SAVIOUR
     
  7. corpcooga

    corpcooga

    Joined:
    Aug 7, 2021
    Posts:
    1
    I have spent the entire day and have finally found this! too op
     
  8. theHeathermoon

    theHeathermoon

    Joined:
    Aug 4, 2021
    Posts:
    2
    this is amazing, thank you!
     
  9. philiphansson

    philiphansson

    Joined:
    Sep 14, 2021
    Posts:
    1
    Haha the code comments was a mess to read, but the solution was ok =) thnx
     
  10. ArazIbrahim

    ArazIbrahim

    Joined:
    Jan 26, 2022
    Posts:
    2
    Thank you! after i changed Public AudioMixer to AudioMixerGroup , it's working.