- Home /
How to play two sounds at once on same object
I have 3 audio sources on my lantern object, each one has the different sound in, and then i have my script file which is below. When i turn the lantern on it only plays the burning sound and not the SoundTurnOn as well?
var soundTurnOn : AudioClip;
var soundTurnOff : AudioClip;
var burning : AudioClip;
var toggle : System.Boolean;
function Start() {
audio.clip = burning;
audio.Play();
}
function Update() {
if (Input.GetKeyDown(KeyCode.F)) {
toggle = !toggle;
if (toggle)
{
audio.clip = soundTurnOff;
audio.Play();
}
else
{
burningfunction ();
}
}
}
function burningfunction () {
audio.clip = soundTurnOn;
audio.Play();
audio.clip = burning;
audio.Play();
}
Answer by Booperkit · Oct 05, 2015 at 05:52 PM
It is possible to have multiple audio sources on one game object.
Answer by zombience · Aug 16, 2013 at 04:31 PM
GameObjects may only have one audio source, and each audio source may only play one clip at a time. If you want to achieve multiple sounds, you may do something like:
create a prefab of an empty game object with nothing on it but an audiosource.
instantiate it in your script, assign the clip you'd like to play, and make it a child of your current gameobject (so it's location will follow your game object).
start playing both audiosources.
This is how I've done it in the past. Again, it is not possible to play multiple clips simultaneously from one audio source, and it is not possible to have mutliple audio sources on one game object
Read through this http://answers.unity3d.com/questions/11314/audio-or-music-to-continue-playing-between-scene-c.html and then my follow up question; what if we add like 10 child game objects to that singleton game object; could we then use 10 audio sources simultaneously throughout our game?
The above answer is old. I'm not sure if it was correct back then either. $$anonymous$$ultiple AudioSource components on one GameObject do work just fine.
@Pharan you're right, it is possible to add more than one per game object. I created this answer way back when I was first learning unity and didn't know better.
Now that I've gotten to know a little more, though, unless you have a special use case, I wouldn't really recommend putting multiple audio sources on one game object. It would be harder to manage if you're assigning clips in the inspector, and I wouldn't necessarily trust that they were being returned in order top down from GetComponents. You would also not be able to separate audio sources by audio channel (in unity 5), and they'd all be constrained to one channel in the audio mixer.
You also wouldn't be able to fade in between clips if these are looping or longer audio clips. Fades would have to be baked into each audio clip. Perhaps one useful scenario for this might be a bomb prefab that had several layers of audio that could all be run at once, and be destroyed at the same time... but if you're going to do that, it's more efficient to bake all of those into one audio file.
So, it's technically possible to put multiple audio sources on one game object, but I'm not really sure why you'd want to do that in most cases.
Your answer
Follow this Question
Related Questions
Audio Playing upon death 1 Answer
How to Play a Sound from an Array from an Overloaded Method? 1 Answer
gameObject to play sound on deletion, won't work... 1 Answer
audio trigger keydown 1 Answer
How To play sound with a keypress 2 Answers