silly simple sound question

Hi,

I’m new to Unity and I have a simple question. I did a forum search, but didn’t find what I was looking for.

I want a script to play a sound when the player hits an object. I have this script:

var ImpactSound : AudioClip;
function OnControllerColliderHit (hit : ControllerColliderHit) {
	if (hit.gameObject.tag == "Barrel") {
		audio.PlayOneShot(ImpactSound);
	}
}

And it does the job, but the problem is: whenever the player hits the object, the sound is played two or three times, starting just a few milliseconds after one-another. The result being some weird flanger effect. Does anybody know why this is happening?

I use Unity 2.6 Indie on a Mac

Thanks in advance,
Hans Willem

simpliest way is make table of sounds lenght, waiting procedure and flip some variable if sound is playing or not. Better would be having some return value from method call

Thanks for the quick reply. I think I understand the first suggestion you’re giving me, but I have trouble understanding the second one. What do you mean by “having some return value from method call”? Could you clarify?

Greetings,
Hans Willem

That was just notice about we dont have but would be nice :slight_smile: Its called synchronization object. Bcause he is missing you must make your own. If you play more sounds then here would be good way making singleton, or just class where you define sound name and lenght in constructor and then you just call play method. Checking if sound is playing can make class :slight_smile: