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


}
}

The audiosource does not have looping selected

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.GetMouseButtonDown(0)) { GetComponent.<AudioSource>().PlayOneShot (sound); Debug.Log ("Hi"); } }

1 Answer

1

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.