Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
5
Question by crocodile5 · Apr 15, 2012 at 05:22 PM · audioaudiosourceaudioplay

How to play multiple audioclips from the same object?

I have a spaceship object and I want to play ambient sound, engine sounds and weapon sounds from the same object. But so far I only could add empty game objects to the spaceship and setting each one as audio source and play the sounds that way.

Is there an easy way to do it without creating multiple child objects?

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

4 Replies

· Add your reply
  • Sort: 
avatar image
13
Best Answer

Answer by aldonaletto · Apr 15, 2012 at 05:46 PM

You can attach several AudioSources to the same object in the Inspector, and get them in an array at Awake with GetComponents(AudioSource) - but knowing who's who may become a problem, since there's no component identifier.
A better alternative is to add the AudioSources at Awake - the script SoundController.js in the Car Tutorial adds as many as 10 AudioSource components to the car!
You could do something like this:

 // define the audio clips:
 var clipAmb: AudioClip;
 var clipEngine: AudioClip;
 var clipWeapon1: AudioClip;
 var clipWeapon2: AudioClip;
 
 private var audioAmb: AudioSource;
 private var audioEngine: AudioSource;
 private var audioWeapon1: AudioSource;
 private var audioWeapon2: AudioSource;
 
 function AddAudio(clip:AudioClip, loop: boolean, playAwake: boolean, vol: float): AudioSource {
   var newAudio = gameObject.AddComponent(AudioSource);
   newAudio.clip = clip;
   newAudio.loop = loop;
   newAudio.playOnAwake = playAwake;
   newAudio.volume = vol;
   return newAudio;
 }
 
 function Awake(){
   // add the necessary AudioSources:
   audioAmb = AddAudio(clipAmb, true, true, 0.2);
   audioEngine = AddAudio(clipEngine, true, true, 0.4);
   audioWeapon1 = AddAudio(clipWeapon1, false, false, 0.8);
   audioWeapon2 = AddAudio(clipWeapon2, false, false, 0.8);
 }

You can then use the audio sources like this:

   audioWeapon1.Play();
   audioEngine.pitch *= curVelocity/maxVelocity;




Comment
Add comment · Show 4 · 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 crocodile5 · Apr 15, 2012 at 05:50 PM 0
Share

Thanks that worked very well

avatar image Griffo · May 27, 2013 at 11:59 AM 0
Share

I came across this script and find it very useful, but what I'd like to do is add rolloff$$anonymous$$ode to the function, the way I thought it would be done is by adding a String value -

 function AddAudio(clip:AudioClip, loop: boolean, playAwake: boolean, vol: float, rollOff: String, maxDis: float): AudioSource {

then adding this to the function -

 newAudio.rolloff$$anonymous$$ode = AudioRolloff$$anonymous$$ode.rollOff;

But it wont accept that, it says 'rollOff' is not a member of 'UnityEngine.AudioRolloff$$anonymous$$ode'. why?

avatar image Griffo · May 27, 2013 at 01:00 PM 0
Share

I've done it this way, don't know if it's the correct way but it works,

 audioWalk = AddAudio(walk, false, false, 1.0, AudioRolloff$$anonymous$$ode.Custom, 60);
 
 function AddAudio(clip:AudioClip, loop: boolean, playAwake: boolean, vol: float, rollOff: AudioRolloff$$anonymous$$ode, maxDis: float): AudioSource {
 
 newAudio.rolloff$$anonymous$$ode = rollOff;
avatar image caseyweeksdotcom · May 12, 2014 at 09:13 AM 0
Share

This helped a ton, thanks! Especially the part about why you would do it this way ins$$anonymous$$d of an array.

avatar image
14

Answer by Demonicdaron · Aug 09, 2015 at 02:43 PM

I've simply sorted the text from above and also included a C# version of the same:

JS:

 // define the audio clips
 var clipAmb: AudioClip;
 var clipEngine: AudioClip;
 var clipWeapon1: AudioClip; 
 var clipWeapon2: AudioClip;
 
 private var audioAmb: AudioSource;
 private var audioEngine: AudioSource;
 private var audioWeapon1: AudioSource;
 private var audioWeapon2: AudioSource;
 
 function AddAudio(clip:AudioClip, loop: boolean, playAwake: boolean, vol: float): AudioSource{ 
     var newAudio = gameObject.AddComponent(AudioSource);
     newAudio.clip = clip; 
     newAudio.loop = loop;
     newAudio.playOnAwake = playAwake;
     newAudio.volume = vol; 
     return newAudio; 
 }
 
 function Awake(){
     // add the necessary AudioSources:
     audioAmb = AddAudio(clipAmb, true, true, 0.2);
     audioEngine = AddAudio(clipEngine, true, true, 0.4);
     audioWeapon1 = AddAudio(clipWeapon1, false, false, 0.8);
     audioWeapon2 = AddAudio(clipWeapon2, false, false, 0.8); 
     } 

You can then use the audio sources like this:

    audioWeapon1.Play();
    audioEngine.pitch *= curVelocity/maxVelocity;

C#:

 // define the audio clips
 public AudioClip clipAmb;
 public AudioClip clipEngine;
 public AudioClip clipWeapon1; 
 public AudioClip clipWeapon2;
 
 private AudioSource audioAmb;
 private AudioSource audioEngine;
 private AudioSource audioWeapon1;
 private AudioSource audioWeapon2;
 
 public AudioSource AddAudio(AudioClip clip, bool loop, bool playAwake, float vol) { 
     public AudioSource newAudio = gameObject.AddComponent(AudioSource);
     newAudio.clip = clip; 
     newAudio.loop = loop;
     newAudio.playOnAwake = playAwake;
     newAudio.volume = vol; 
     return newAudio; 
 }
 
 public void Awake(){
     // add the necessary AudioSources:
     audioAmb = AddAudio(clipAmb, true, true, 0.2);
     audioEngine = AddAudio(clipEngine, true, true, 0.4);
     audioWeapon1 = AddAudio(clipWeapon1, false, false, 0.8);
     audioWeapon2 = AddAudio(clipWeapon2, false, false, 0.8); 
     } 

You can then use the audio sources like this:

    audioWeapon1.Play();
    audioEngine.pitch *= curVelocity/maxVelocity;


Comment
Add comment · Show 3 · 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 adrianov · Jan 28, 2016 at 07:27 PM 0
Share

I tried your method, seemed exactly what I needed, but I'm having an issue with it compiling correctly. In the AddAudio method, the "public" declaration on the Audiosource newAudio is throwing an error. I'm gonna dig through the webs for a solution, but if you have any ideas please post them. Thanks!

avatar image adrianov · Jan 28, 2016 at 08:05 PM 2
Share

So I messed around with it a bit and it looks like for C# you need to write it like this in order to not get any errors, at least this worked for me. Thanks again for the code, it works beautifully now!

     public AudioSource AddAudio (AudioClip clip, bool loop, bool playAwake, float vol) {
 
         AudioSource newAudio = gameObject.AddComponent<AudioSource>();
 
         newAudio.clip = clip;
         newAudio.loop = loop;
         newAudio.playOnAwake = playAwake;
         newAudio.volume = vol;
 
         return newAudio;
 
     }
avatar image HelloNewman · Sep 02, 2016 at 08:48 PM 0
Share

You also need to add a correct float value to each AddAudio(). They need a f beside every float value: ex. 0.2f

avatar image
3

Answer by FFozzy · Mar 17, 2016 at 08:38 PM

There's also a PlayOneShot(AudioClip clip, float volumeScale) function that seems to be playing overlapping sounds as I use it right now. I'm playing pretty short clips, but they seem to be playing simultaneously.

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
avatar image
1

Answer by sasanostudio · Feb 09, 2018 at 04:20 PM

Short:

If you have some AudioClips clip1 and clip2 that you want to play from an AudioSource on the GameObject gameObject1, you can use: gameObject1.GetComponent().PlayOneShot(clip1); gameObject1.GetComponent().PlayOneShot(clip2);

Long:

If you want to store a set of audio clips and play them without dealing with uploading them in your other scripts, here's a script called MoreAudioClips. 1. Attach MoreAudioClips script to a GameObject with an AudioSource. 2. Upload your AudioClips in the Inspector view of MoreAudioClips. You can also modify the volumes if you want, but they default to 1. 3. Play the AudioClips from elsewhere by using: GetComponent().PlayClip[2]; to play the 3rd clip in the list, or GetComponent().PlayRandomClip(); to play a random clip in the list.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 [RequireComponent(typeof(AudioSource))]
 public class MoreAudioClips : MonoBehaviour {
   /// <summary>
   /// MoreAudioClips: stores a list of audio clips so that they can be played from an audio source on the gameobject
   /// </summary>
   /// Attached to: a gameobject that has an audio source and you want to use multiple audio clips for
     public List<AudioClip> clips; // store the audio clips
     public List<float> volumes;
 
     void Start()
     {
         while (volumes.Count < clips.Count) //ensure volumes are valid for the clip indices
         {
             volumes.Add(1f); // set a default volume of 1 for clips with no specified volume
         }
     }
 
 
     public void PlayClip(int clipNum)
     { // used to play a specific clip from another script
       // GetComponent<MoreAudioClips>().PlayClip[2] would play the 3rd clip (index 2) that you set in the Inspector list for MoreAudioCLips
         if (clips.Count > 0 && clipNum >= 0 && clipNum < clips.Count)
             GetComponent<AudioSource>().PlayOneShot(clips[clipNum], volumes[clipNum]); //uses the AudioSource on the current gameObject
     }
     
     public void PlayRandomClip()
     { // used to play a random clip from the set of clips
         if (clips.Count > 0)
         {
             int clipNum = Random.Range(0, clips.Count - 1);
             GetComponent<AudioSource>().PlayOneShot(clips[clipNum], volumes[clipNum]);
         }
     }
 
 }
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

13 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

Related Questions

Function Update play sound once 1 Answer

Audio Distortion effects? 0 Answers

Add Audio Source to a Prefab 2 Answers

2nd Audio Clip Not Playing 0 Answers

Need help with audio files causing unity to freeze 0 Answers


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