• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Dan Jimenez · Aug 30, 2010 at 12:59 PM · guibuttonaudiosoundclip

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")){

} }

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

7 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by jashan · Aug 30, 2010 at 01:39 PM

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
-1
Wiki

Answer by Quest · Sep 30, 2010 at 05:54 PM

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by devilkkw · Sep 30, 2010 at 06:05 PM

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();

} }

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by Quest · Oct 04, 2010 at 05:00 PM

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 ?

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image charnew · Dec 25, 2011 at 05:19 PM 0
Share

here's what I did:

var model : GameObject; var track : AudioClip;

function OnGUI () {

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

   audio.PlayOneShot(track);

} //$$anonymous$$ake 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: $$anonymous$$issingComponentException: There is no 'AudioSource' attached to the "$$anonymous$$ain Camera" game object, but a script is trying to access it. You probably need to add a AudioSource to the game object "$$anonymous$$ain Camera". Or your script needs to check if the component is attached before using it.

avatar image
0

Answer by Adam Rademacher · Oct 04, 2010 at 05:22 PM

var track : AudioClip;
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
  • 1
  • 2
  • ›

Your answer

Hint: You can notify a user about this post by typing @username

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

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

play an audio clip from a GUI button 1 Answer

Play sound on button click before loading level. 3 Answers

Buzzing noise instead of my audio-clip? 0 Answers

Help with sound clips [C#] 0 Answers

Audioclip timesample to seek start position but not the end 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges