- Home /
Playing audio when a button is clicked?
I am currently using Unity Javascript and am trying to get an audio clip to play when I click on a button for the main menu. I have background music, so I figured I could put a second audio source on the main camera (where the GUI and audio is located for me) and write some line of code within an if block for the button. However, I do not currently know of a way to play that sound, I know it has something to do with either AudioSource or AudioClip. (I am a new programmer if you can't tell) What would need to be used to get this audio file to play when clicked?
Answer by CgShady · Sep 20, 2012 at 11:13 PM
Hi,
you can add the following to you script.
this.GetComponent(AudioSource).Play();
You will need to add an AudioSource component with the sound you want to play in it.
Hope this helps.
Thanks for the help, I ended up putting the background music and throwing that on a different object as an audio source to use what you told me to do. :)
Your answer
Follow this Question
Related Questions
Audio loop after time 2 Answers
I am having an issue with returning an array 2 Answers
3 sets of button activations GUI 1 Answer
Is it possible to link character skill lists to a GUI, and if so, how? 3 Answers
A node in a childnode? 1 Answer