- Home /
Help With GUI Stop and play other Sound ?
Hi guys, i have created this script it basically plays a song when clicked on and that's all good but i would like to ask your help with something. what i want to do is if i click on my GUI button while the song is playing i want it to stop the track. This is if i click on the same GUI if the music is playing.
Here is the script :
var MyAudioTrack1Naruto : AudioClip; //var MyAudioTrack2Naruto : AudioClip; //var MyAudioTrack3Naruto : AudioClip; //var MyAudioTrack4Naruto : AudioClip; //var MyAudioTrack5Naruto : AudioClip;
function OnGUI () {
//Make the first button
if(GUI.Button (Rect(10, 20,105,20), "Play Track 1 (Naruto Enter) ")){
if (!audio.isPlaying){
audio.Stop(); //if something else is playing stop it and then play track
audio.clip = MyAudioTrack1Naruto;
audio.PlayOneShot(MyAudioTrack1Naruto);
}
}
}
Oh and thanks in advance :)
Answer by Jake-L · Feb 24, 2011 at 08:26 AM
In your inner condition you call audio.Stop() only when audio.isplaying==true!
Try this:
var myTrack : AudioClip;
function OnGUI() { if (GUI.Button(Rect(10,20,105,20),"Start/Stop Track 1")) { if (audio.isPlaying) audio.Stop(); audio.PlayOneShot(myTrack); } }
Your answer
Follow this Question
Related Questions
Play sound on button click before loading level. 3 Answers
A node in a childnode? 1 Answer
Unity disables my Sound Card 1 Answer
How to stop Audio not all?? 0 Answers
Play Sound when GUI button is pressed. 7 Answers