- Home /
Audio Play/Stop Trouble
Hi, Im having trouble with my sound on my game. i have it where when you hit an invisible wall then music starts to play. but i want music playing before that to stop playing and my new music to start. so basically i need one audio clip to stop and a different one to start at the same time. Thanks for any help!
Answer by Jesus_Freak · Nov 09, 2010 at 10:29 PM
here's a script from the unity scripting reference. it stops an already playing sound or music file after it's fully done playing, pauses it, then plays a sound file.
// Play default sound audio.Play();
// Wait for the audio to have finished yield WaitForSeconds (audio.clip.length); audio.Pause();
// When the audio component has stopped playing, play otherClip var otherClip : AudioClip;
function Update() { if(!audio.isPlaying) { audio.clip = otherClip; audio.Play(); } }
you could modify it so that it plays and stops the sound when you hit that wall:
var Sound2 : AudioClip;
function OnTriggerEnter(hit : collider)
{
if(hit.gameObject.tag == "PlayStop")
{
audio.pause;
audio.clip = Sound2;
audio.Play(Sound2);
}
}
that last one might not work, but i know the top one works perfectly. and don't forget to tag your wall "PlayStop" !!!!
okay. to pause an already playing sound, just do
var Sound1 : AudioClip; var Sound2 : AudioClip;
function OnTriggerEnter(hit : Collider) { if(hit.gameObject.tag == "PlayStop") { audio.Pause(Sound1); audio.Play(Sound2); } }
and actually, i just read, you could just add a pause statement to the original sound. i'll add that to my answer. (5:32)
Your answer