Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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.

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

Making sounds wait for each other to finish 2 Answers

Random, 3d sound. 1 Answer

audio.clip.length 'hides' my button! yaiks! 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges