- Home /
Can I play multiple AudioSources from one gameobject?
I've searched and read many different posts and still haven't been able to accomplish this. Ideally I'd like to attach several different AudioSource components to a game object and be able to trigger them from a script attached to that object.
I've tried several different things and no matter which method I try, there seems to be a different issue keeping me from being able to play the sounds properly.
1- I want to be able to set some sounds to loop and others to not loop. Because of this, I need to create Audio Sources and not just use audio.PlayOneShot or AudioSource.PlayClipAtPoint. (Not to mention that I want to be able to set other parameters easily, such as the 3D settings and priority, volume and pitch.)
2- I need all of the sounds to be 3D positioned on the game object, so creating a game object for each sound and calling them in the script using TireRollLoop.Play(); for example, will not work, as the sounds are tied to their own game object's position in the scene.
3- Using audio.clip = TireRollLoop; to set the sound and then playing the sound using audio.Play() does not work because I need to be able to play some sounds at the same time.
Sorry for the lengthy question but I've been running into all sorts of issues. I am not a programmer but can usually make things work by trial and error.
This is already answered but one more thing left: with drag and drop of audioclip (as you could see in unity video tutorials) you can assign only one audiosource for a gameobject. If you drag and drop second audioclip - second audiosource will not be created and just clip will be changed in audiosource reference. So for adding next one audiosource for same gameobject you have to choose AddComponent->Audio->AudioSource
Answer by aldonaletto · Oct 12, 2011 at 09:53 PM
That's a good question! You can add several AudioClips to the same object, and get them with GetComponents:
var audio1: AudioSource;
var audio2: AudioSource;
var audio3: AudioSource;
function Start(){
var aSources = GetComponents(AudioSource);
audio1 = aSources[0];
audio2 = aSources[1];
audio3 = aSources[2];
}
// you can then access the different AudioSources using the variables you've set:
audio2.Play();
audio1.Stop();
NOTE: Presently, the AudioSources are added to the array in the order they appear in the Inspector, as we could expect. Unfortunately, nobody knows if this order may be different in future Unity versions. If you want to be sure about that, you can set different Priority values (for instance) and check them in Start.
This is GREAT!!! You've helped me with a couple questions on this site and I really appreciate it! It works perfectly! The other path I was going down was to create a game object for every sound and then have the sound's game object follow the game object I want it positioned to. This is a much better solution! $$anonymous$$y next issue is going to be randomizing audio using an array and I'm wondering how this will work since I'm already using an array but I'll cross that bridge when I get there. Thanks again! Here's what I did to test it:
var SkateboardRoll: AudioSource;
var SkateboardLand: AudioSource;
var SkateboardJump: AudioSource;
function Start(){
var aSources = GetComponents(AudioSource);
SkateboardRoll = aSources[0];
SkateboardLand = aSources[1];
SkateboardJump = aSources[2];
SkateboardRoll.Play();
yield WaitForSeconds (1);
SkateboardLand.Play();
yield WaitForSeconds (1);
SkateboardJump.Play();
}
If you want one of these audio sources to emit random sounds, you can create an AudioClip array, set its size and assign its elements at the Inspector, then select a random clip prior to play it:
var sounds: AudioClip[]; // fill this array in the Inspector var SkateboardRoll: AudioSource; ... // choose a random sound to play: SkateboardRoll.clip = sounds[Random.Range(0, sounds.length)]; SkateboardRoll.Play(); ...
Thanks again for the help!
I noticed that I didn't have to set the audio source variables on the script in the inspector. As a test, I commented out the three variables from the script and it doesn't look like I need them since the sounds still play.
It looks like all I have to do is create the audio source components and assign them each a clip, create the array of different audio sources in the script, then call the audio source I want to play.
//var SkateboardRoll: AudioSource;
//var SkateboardLand: AudioSource;
//var SkateboardJump: AudioSource;
function Start(){
var aSources = GetComponents(AudioSource);
SkateboardsRoll = aSources[0];
SkateboardsLand = aSources[1];
SkateboardsJump = aSources[2];
SkateboardsRoll.Play();
yield WaitForSeconds (1);
SkateboardsLand.Play();
yield WaitForSeconds (1);
SkateboardsJump.Play();
}
When you removed the variable declaration, the compiler created the variables itself inside Start. The problem here is that these variables are temporary: they die as soon as you leave the function. You can't use them in Update, for instance; the compiler may even create the variables inside Update, but they will be brand new instances, and you will have a Null Reference error when trying to use them.
I don't see much reasons to do such assignments:
audio1 = aSources[0];
As you can play it that way:
aSources[0].Play();
The only way to use it is to give a proper name like
coin = aSources[0];
coin.Play()
But from programer's point of view getting array and then assign every array element to variable of same type looks like a nonsense ;)
Answer by anear · Nov 17, 2013 at 01:08 AM
if you use c#, the code is
AudioSource correctAudio;
AudioSource errorAudio;
void Start() {
AudioSource[] audios = GetComponents<AudioSource>();
errorAudio = audios[0];
correctAudio = audios[1];
}
void OnGUI() {
if (answer == currAnswer)
correctAudio.Play();
else
errorAudio.Play();
}
I was confused about playing multiple audio sources myself. This was exactly what I was searching for. Thanks!
This helped play 2 sounds on the same object. Thanks!
Answer by maxus · Jul 26, 2015 at 05:31 PM
The easiest way to manage multiple audio sources is to have public references to them and assign them in the inspector:
Add multiple audio source components to your GameObject
Create a public AudioSource variable for each one
Assign their values by dragging the AudioSource component into the variable slot in the inspector
Access them directly from code
You! I don't know why this concept is SO HARD to remember for having multiples of things... Thank you for being the only person to re$$anonymous$$d me after an hour of searching! When in doubt?? Add it in the inspector! Thanks!
Answer by Java666 · Nov 29, 2013 at 12:04 AM
i did only
public AudioSource correctAudio;
then where i wanted sound to play i did
correctAudio.Play();
only thing is you need to have GameObject created with AudioSource on it and add to the public spot. doing it this way allows me not to use as much coding and can add as many sounds as i like to play i'm just not sure how much space would be needed this way if its more or less than other way i know using AudioClip.
Answer by aviomaksim · Jul 08, 2014 at 03:44 PM
Just create child objects with different AudioSource's and link code there.
Your answer
Follow this Question
Related Questions
Multiple Audio Source Problem 1 Answer
How can I play multiple audioclips from the same object? 2 Answers
Playing a sound clip from another object 1 Answer
Buzzing Audio 1 Answer