- Home /
Choose from 2 audio Sources
Hello,
i have an object with the audio source component twice. How can I choose what of the two to play?
I tried using this, but it doesn't work
audio.Play("Name");
Answer by gregzo · Aug 01, 2013 at 01:45 PM
You can use GetComponents ( be careful, it will return an array of components, which you then have to individualy cast as AudioSources ).
Or, which is what I'd advise and do, add the AudioSource component by code :
public int nbOfSources;
AudioSource[] sources;
void Awake()
{
for ( int i = 0; i < nbOfSources; i++ )
{
sources[i] = gameObject.AddComponent ( typeof ( AudioSource ) ) as AudioSource;
//add setup of each source here : audioclip, loop, dopplerLevel, volume etc... )
}
}
Answer by roojerry · Aug 01, 2013 at 01:29 PM
I wouldn't have thought that you could add 2 audio sources to the same object. Calling it the way you are would assume that there is just one audiosource attached. However, there is a GetComponents call that will return all components of a type. Ive never used it, but that would seem to be what you want. Either that, or just make separate child objects each containing an audiosource. That way you could call child1.audio.Play()
and child2.audio.Play()
You can have 20 AudioSources on one object. Very convenient if you need to implement your own s$$anonymous$$l behaviour, for example.
Your answer
Follow this Question
Related Questions
Best way to play audio sound effects 1 Answer
Guys i need help with audio! 0 Answers
Randomly play audio 0 Answers
Audio source not playing 7 Answers