x


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 :)

more ▼

asked Feb 24 '11 at 04:40 AM

MC HALO gravatar image

MC HALO
878 74 94 111

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

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);
  }
}
more ▼

answered Feb 24 '11 at 08:26 AM

Jake L. gravatar image

Jake L.
943 5 6 22

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x3737
x3684
x1027
x524
x72

asked: Feb 24 '11 at 04:40 AM

Seen: 1245 times

Last Updated: Feb 24 '11 at 04:40 AM