- Home /
Multiple audio samples on one object.
I need your help. I'm trying to learn C#. I want to do that on a one single object were three audio samples and when I click on object with mouse button - sounds were played back in sequence. First click = sound 1, second click = sound 2, third click = sound 3...and again first click = sound 1.
I'm trying to make this so:
public AudioClip sound1;
public AudioClip sound2;
public AudioClip sound3;
void OnMouseDown ()
{
audio.PlayOneShot(sound1);
}
But I don't understand how to play another two audio in sequence. I also need that when I released the mouse button the sound faded using 1 second audio fade-out. How can I make this fadeout?
Help me please.
Answer by save · Oct 23, 2013 at 02:17 PM
Put the AudioClip's in a built-in array and use a pointer which you increment and reset when it has reached the length of the AudioClip array. Make use of coroutines to play, fade in and fade out the sound.
Read this article about arrays and how you can work with them. What I usually do when I have a pointer in an array is to increment it with a remainder to reset it to zero when exceeded the length of the array. For instance:
audioClipArray[i].Play();
i++; i=i%audioClipArray.Length;
Next step is volume, an AudioSource can only play one clip at a time as far as I know, so use two AudioSources. During the length of the audio you fade in and fade out the sound, for instance have a look at Duck's answer on this issue to get you started. A coroutine will make excellent use in this case.