Issues getting sounds to play only once, on mouse click

Hello. I’m currently working on a gunshot sound script. I know it’s pathetic to not have a handle on this after how long I’ve been working with Unity; it’s just after upgrading from four to five that I’ve been having issues, as the way audio is being handled has changed, and my old script is broken.

Essentially, the issue that has arisen is that no matter what, the sound continuously plays, no matter what, even skipping over my if statements. I even put it all into it’s own function, and called that into the if statement at one point, to the same result.

I’m not sure if it’s an issue with how I have the audio source setup, or my scripting, since I have little understanding of the new script that’s needed.

Without further ado, here is my script:

function Update () {
var audio: AudioSource = GetComponent.<AudioSource>();


Now, I know it’s a relatively simple script, but it has one simple function, so it should need to be that large. The thing is, the Input if statement there seems to be entirely ignored, and it just keeps playing the sound continuously, which isn’t easy on the ears.

I know previously in Unity Four, you had the option of .PlayOneShot, but that seems to have been removed? I know in the inpector I don’t have loop checked, so that’s not the issue. Can anyone help me revise my script, or point out the mistakes I’ve made?

Thanks for any help in advance. I look forward to reading your replies. With much love, Jaxsonp10

Oh my god. I think I might be retarded. There is a semi-colon at the end of my if statement. I removed it. It works fine.

Try this :wink: