- Home /
How to string multiple audio clips together
Hi guys,
I need to string three audio clips together to give a score at the end of the game. For example, I've got a clip which says "You've got", several with a number - e.g. "two" and finally "stars!".
Obviously, the number audio clip is the player's score, stored as an int variable. Would any of you clever people be able to suggest how I can play each in turn so that it sounds like a sentence?
I've only been using Unity for a couple of weeks (and programming for as long!), so be gentle with me!
Many thanks
Answer by fherbst · Jul 05, 2010 at 08:15 PM
You can use something like this:
var startClip: AudioClip; // make sure to attach all your number audio clips here var numberClip:AudioClip[]; var endClip: AudioClip;
function PlayNumberSound(i : int) { audio.clip = startClip; audio.Play(); yield WaitForSeconds (audio.clip.length); audio.clip = numberClip[i - 1]; // i-1 because array starts at 0 audio.Play(); // you can probably leave this one out yield WaitForSeconds (audio.clip.length); audio.clip = endClip; audio.Play(); // you can probably leave this one out yield WaitForSeconds (audio.clip.length); audio.Stop(); }
function Start() { // Should play startClip + numberClip[5-1] + endClip PlayNumberSound(5); }
I haven't tested it, but it might work :).
Thank you Felix, you clever chap! I really appreciate this. It works perfectly (after I remembered to attach an audio source to the game object the script was attached to!). Now I need to work out some way of accessing the array based on the value of a string variable!