- Home /
On Mouse Click Play Sound
hello I have developed a script to play a sound when the mouse is clicked. I have attached that script to an empty game object with an audio source with the sound that I want to play. However when I begin the game the sound plays and when I click nothing happens. I will post the script below and if anyone could help me that would be awesome. Thanks
var sound : AudioClip;
function Update () {
if (Input.GetMouseButtonDown(0)) {
GetComponent.<AudioSource>().PlayOneShot (sound);
}
}
Also check if AudioSource doesn't have Looping checkbox checked
this script is actually on a gameobject in your scene with an Audiosource component attached to it?
Hi,
Can you add a Debug.Log into the if(){} to see if something happens when you click ?
Yes the script is attached to a gameobject in the scene with an audio component attached. And as regards for the debug.log, I start the scene and I hear the sound then when I click I see a debug.log message. Here is my updated script. Thanks for all the helps
var sound : AudioClip;
function Update () {
if (Input.Get$$anonymous$$ouseButtonDown(0)) {
GetComponent.<AudioSource>().PlayOneShot (sound);
Debug.Log ("Hi");
}
}
Answer by Notter · May 20, 2015 at 12:19 PM
If you've attached the audio clip to the source, you need to do:
GetComponent.().Play();
if you use: PlayOneShot(yourClipName); you need to pass the clip to the script, not the audio source.
Your answer
Follow this Question
Related Questions
Destroying Collected Objects / Playing Audio 2 Answers
Audio loop after time 2 Answers
Sound playing at random. (JS) 2 Answers
play sound while at certain coordinates of map (javascript) 1 Answer
Why does the audio sound different than the original ? 0 Answers