- Home /
How to play multiple audio clips in a row?
Hi here's what I want to do. I have a scene where I want my script to loop through multiply audio clips in a row that I assign via the inspector. I'm currently using this script(Script1) to play the audio clip thats assigned to the audio source attached to my game object and then assign the next audio clip once the first one ends and Im using the yield WaitForSeconds to make sure it plays through the entire audio clip before assigning the new one to the audio source. But this isn't what I want so I modified the script(Script2) so that it will loop through 3 audio clips, but it won't work at all and the only error that displays is this one: Script error (audio): Update() can not be a coroutine. Please help! Thanks (:
Script1
#pragma strict
@script RequireComponent(AudioSource)
public var soundClip1 : AudioClip;
// Play default sound
function Start()
{
audio.Play();
// Wait for the audio to have finished
yield WaitForSeconds (audio.clip.length);
// Assign the other clip and play its
audio.clip = soundClip1;
audio.Play();
}
Script2
#pragma strict
@script RequireComponent(AudioSource)
public var soundClip1 : AudioClip;
public var soundClip2 : AudioClip;
public var soundClip3 : AudioClip;
// Play default sound
function Update ()
{
if(Input.GetKey("m")) {
audio.Play();
// Wait for the audio to have finished
yield WaitForSeconds (audio.clip.length);
// Assign the other clip and play its
audio.clip = soundClip1;
audio.Play();
}
else if(Input.GetKey("m")) {
audio.clip = soundClip2;
audio.Play();
yield WaitForSeconds (audio.clip.length);
}
}
Answer by KiraSensei · Dec 03, 2013 at 08:02 PM
"Update() can not be a coroutine" means that you cannot call WaitForSeconds into the Update() method.
So I suggest you to do the following code if you want to keep the same logic :
#pragma strict
@script RequireComponent(AudioSource)
public var soundClips : AudioClip[3];
private var musicNumber:int = 0;
private var playNextMusic:boolean = true;
// Play default sound
function Update ()
{
if(Input.GetKey("m")) {
if (playNextMusic) PlayTheNextMusic();
}
}
function PlayTheNextMusic() {
playNextMusic = false;
audio.clip = soundClips[musicNumber];
audio.Play();
yield WaitForSeconds (audio.clip.length);
playNextMusic = true;
++musicNumber;
if (musicNumber == AudioClip.Length) musicNumber = 0; // I'm not sure if it is Length or length here...
}
okay I pasted your code in and commented out $$anonymous$$e but now its telling me its expecting a semicolon on line 9 and 34 spaces over which is after the AudioClip[3]; array your making?
Oh sure, I didn't see my mistake :
public var soundClips:AudioClip[] = new AudioClip[3];
Can I make this loop through as many audio clips as I want?
Okay thanks it works great with switching through the clips but is there anyway that once the m key is clicked it just loops through all the audio clips but it would be cool if it still switched to the next one when the user clicked the m key but it doesn't just stop when the clip is over it actually moves on to the next audio clip and so on?
Of course, you just have to create your array as long as you need, or use lists and add them one by one. Here the code is for 3 but you can put 10, 100 audio clips if you want.
You may also use the Random function to randomly play them.
Answer by twobob · Nov 21, 2014 at 02:15 PM
"I don't know why the code above would not work."
if (musicNumber == AudioClip.Length) musicNumber = 0;
should be
if (musicNumber == soundClips.Length) musicNumber = 0;
would be my guess at first glance.