x


Play Sound when GUI button is pressed.

Hello, I am trying to have a quick sound play when I press a GUI Button Here is the script that I have for the GUI buttons. Is there an easy way to attach a sound to each button.

var model : GameObject;

function OnGUI () { 

  //background box 
  GUI.Box (Rect (10,130,100,90), "Sounds");

  //Make first button
  if (GUI.Button (Rect(20,160,80,20),  "Go")){

  } 

  //Make second button
  if (GUI.Button (Rect(20,190,80,20),  "Stop")){ 

  } 
}
more ▼

asked Aug 30 '10 at 12:59 PM

Dan Jimenez gravatar image

Dan Jimenez
20 13 13 18

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

5 answers: sort voted first

It is in fact very simple: All you need to do is attach an AudioSource to the same game object (or any other game object), provide a "slot" for that audio source, e.g.

var audio : AudioSource;

And then call

audio.PlayOneShot(audio.clip);

in your if-blocks. If you want multiple sounds, simply create one game object with relevant audiosources attached for each sound you want to play (and create var audio1 : AudioSource; and so on, or use names that actually will tell you what the sound means).

more ▼

answered Aug 30 '10 at 01:39 PM

jashan gravatar image

jashan
10.1k 25 40 116

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

uhmm got a little problem where to place the audio.PlayOneShot(audio.clip); ??? please help this sad guest

more ▼

answered Sep 30 '10 at 05:54 PM

Quest gravatar image

Quest
1

(comments are locked)
10|3000 characters needed characters left
var model : GameObject;
var track : AudioSource;

function OnGUI () { 

  //background box 
  GUI.Box (Rect (10,130,100,90), "Sounds");

  //Make first button
  if (GUI.Button (Rect(20,160,80,20),  "Go")){
   audio.clip = track;
        audio.loop = false;

      audio.Play();

  } 

  //Make second button
  if (GUI.Button (Rect(20,190,80,20),  "Stop")){ 

      audio.Stop();

  } 
}
more ▼

answered Sep 30 '10 at 06:05 PM

devilkkw gravatar image

devilkkw
297 7 10 18

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

uhm really thanks but... i got a error and i dont know what to do can you help me ?

Assets/side-scroller/Scripts/GUI Options Button.js(40,25): BCE0022: Cannot convert 'UnityEngine.AudioSource' to 'UnityEngine.AudioClip'

what to do ?

more ▼

answered Oct 04 '10 at 05:00 PM

Quest gravatar image

Quest
1

here's what I did:

var model : GameObject; var track : AudioClip;

function OnGUI () {

//background box GUI.Box (Rect (10,130,100,90), "Sounds"); //Make first button if (GUI.Button (Rect(20,160,80,20), "Go")){ audio.loop = false;

  audio.PlayOneShot(track);

} //Make second button if (GUI.Button (Rect(20,190,80,20), "Stop")){ audio.Stop(); } }

I added a component which is Audio Source to the main camera, dragged my mp3 to the track variable and I also dragged this one to the AudioClip slot. It worked but my editor log returns this message: MissingComponentException: There is no 'AudioSource' attached to the "Main Camera" game object, but a script is trying to access it. You probably need to add a AudioSource to the game object "Main Camera". Or your script needs to check if the component is attached before using it.

Dec 25 '11 at 05:19 PM charnew
(comments are locked)
10|3000 characters needed characters left
var track : AudioClip;
more ▼

answered Oct 04 '10 at 05:22 PM

Adam Rademacher gravatar image

Adam Rademacher
1.1k 1 3 19

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

x3693
x1029
x789
x525
x83

asked: Aug 30 '10 at 12:59 PM

Seen: 6506 times

Last Updated: Dec 25 '11 at 05:19 PM