system
1
If I have an empty game object with two audio sources, each with a unique audio clip.
How, in script, do I pick one from the other? Let's say one audio source has a audio clip called jump.mp3 and the other has grunt.mp3
is it something like
var grunt : Audio Source;
grunt = audio source with grunt.mp3
And then use grunt to talk to that one?
Or is there a way that a script attached to the game object with the two audio sources can directly talk to a chosen audio source without having to assign a variable?
You can define two different audio sources as public variables and bind them in editor. Just drag the component into the variable slot in the inspector.
var audio1 : AudioSource;
var audio2 : AudioSource;
When you then want to play either of those you can call
audio1.Play();
// or
audio2.Play();
Or if you want you can use GetComponents(AudioSource); to get an array of them, and you can enumerate through them. I think the first solution is easier though.
efge
2
I think you do not have to use multiple AudioSources on one GameObject. From the reference for AudioSource: Multiple sounds can be played on one AudioSource using PlayOneShot.
You could assign your audio clip to a variable (or an array) and use for single events like shots etc.:
var grunt : Audio Source;
audio.PlayOneShot(grunt);
or if you want more control of the AudioClip:
audio.clip = grunt;
audio.Play();
adriana
4
Just a simple question: if I have two AudioSources, can they play together their two different audios ???
In code: if I write
audio1.Play();
audio2.Play();
can all of them play their sound, or only will play audio2 ???
Tnx !!