Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by MathewJProductions · Feb 20, 2019 at 09:01 PM · audioaudiosource

Setting a specific Audio Mixer Group through code

Hey there, I'm trying to create a settings menu where I can adjust the volume of different audio groups but with the way I've made the audio manager I can't figure out a way to create the audio sources with the audio groups attached to them. I'm creating the audio Sources as the scene starts and the audio is played once I trigger something like a jump or when I shoot. But none of the audio Sources have an audio mixer group on them. If anyone could help me out that would be great. The code for the Audio Manager:

     public Sound[] sounds;
 
     // Use this for initialization
     void Awake () {
         foreach (Sound s in sounds)
         {
             s.source = gameObject.AddComponent<AudioSource>();
             s.source.clip = s.clip;
 
             s.source.volume = s.volume;
             s.source.pitch = s.pitch;
         }
     }
     
     public void Play (string name)
     {
         Sound s = Array.Find(sounds, sound => sound.name == name);
         s.source.Play();
     }

The code for The Sound Script:

     public string name;
 
     public AudioClip clip;
 
     [Range(0f, 1f)]
     public float volume;
     [Range(.1f, 3f)]
     public float pitch;
 
     [HideInInspector]
     public AudioSource source;

I hope that made sense ( I'm relatively new to implementing audio ) Thank you for your time.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by MathewJProductions · Feb 20, 2019 at 10:05 PM

For anyone that sees this, I have kind of got it working. I changed the Audio Manager to this:

     public Sound[] sounds;
     public AudioMixer mixer = Resources.Load("GameAudio") as AudioMixer;
 
     // Use this for initialization
     void Awake () {
         foreach (Sound s in sounds)
         {
             s.source = gameObject.AddComponent<AudioSource>();
             s.source.clip = s.clip;
 
             s.source.volume = s.volume;
             s.source.pitch = s.pitch;
 
             string OutputMixer = "SoundEffects";
 
             GetComponent<AudioSource>().outputAudioMixerGroup = mixer.FindMatchingGroups(OutputMixer)[0];
         }
     }
     
     public void Play (string name)
     {
         Sound s = Array.Find(sounds, sound => sound.name == name);
         s.source.Play();
     }

This works but only on the first Audio Source ( i have 8 different ones ). If anyone could help me out with this that would be great, thanks.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image MerlinsMaster · Jan 22, 2020 at 06:55 AM 0
Share

Hey, I recognize this code. It's from the Brackey's tutorial, right? I'm having the same problem. I want different sounds to be controlled by different audio$$anonymous$$ixerGroups. I tried putting "public Audio$$anonymous$$ixerGroup audio$$anonymous$$ixerGroup;" at the beginning of Sound.cs, but it did not work. I have a question regarding the above code. What is the purpose of "[0]" at the end of line 16?

avatar image
0

Answer by Ben_Grange · Jan 27, 2021 at 07:21 PM

@MathewJProductions and @MerlinsMaster — I realize I'm a little late to this post, but if anyone is still looking for help on this:

You've serialized the audio mixer in the AudioManager class. You need to serialize the audio mixer group in the Sound class. Then, once you've done that, you need to iterate the audio mixer group through the array in your AudioManager class. So, it would look like this:

Audio Manager:

     foreach(Sound s in sounds)
     {
         s.source = gameObject.AddComponent<AudioSource>();
         s.source.clip = s.clip;
         s.source.outputAudioMixerGroup = s.audioMixerGroup;

         s.source.volume = s.volume;
         s.source.pitch = s.pitch;
     }

And in the Sound class:

 public class Sound
 {
     public string name;
     public AudioClip clip;
     public AudioMixerGroup audioMixerGroup;
 
     [Range(0f, 1f)] public float volume;
     [Range(.1f, 3f)] public float pitch;
 
     [HideInInspector] public AudioSource source;
 }

Then you can assign the specific audio mixer group in the inspector. Hope this helps!

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

132 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How can I play multiple audioclips from the same object? 2 Answers

Play audio clip without a variable 1 Answer

Is there a way to create a random Audiosource loop? 2 Answers

Audio not playing... 2 Answers

Can't get sound clip from array to play properly 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges