Audio Clip Getting Cut Short

I’m having problems with a few audio clips not playing correctly. All my clips are wav and generally less than 5 seconds long.

My sourcecode is really simple.

#pragma strict
var bonusSound : AudioClip;

function OnTriggerEnter(hit:Collider){
	if(hit.tag == "Player") {	// See if the impact was the player
		if(bonusSound)	// Check if there is a sound clip attached to this bonus
			AudioSource.PlayClipAtPoint(bonusSound, transform.position, 0.1 );	// If there is, play it
	}
}

This is attached to my “bonus” gameObject, which is a prefab and is located twice throughout the level. The first time it plays the audio for the first bonus, it usually works ok. Sometimes it gets cut off half way through. The second bonus almost never plays correctly. It plays for a fraction of a second (maybe 0.1 seconds), sometimes nothing at all.

I should note that I’ve tried both 2D and 3D audio for this clip.

I think it may be related to the number of sound files I have running at one time as when I remove some enemies the problem is much less likely to occur. There is about 80-130 audio clips running most of the time (and when the problem occurs), but there are areas of my level running about 170 audio clips with no problem.

This is on a PC, not a mobile so I would imagine that certainly it should be able to handle a lot more than that.

I should point out that I’ve also tried just using “audio.Play” and attaching the audio clip directly, but that has the same problem. My audio file itself is definitely ok as I’ve been using it for months now.

Is there perhaps a better way that I can be playing the sound files?

It seems that you’re playing too much sounds at the same time. If this is the case, maybe handling the property AudioSource.priority could solve your problem. Unfortunately, you can’t set the priority of sounds played by PlayClipAtPoint because it doesn’t give any reference to the temporary AudioSource created (which has default priority, 128). You could try to set a lower priority (129…256) in the less important sources, or instead create your own PlayClipAtPoint with a higher priority (0…127):

function PlayClipAtPoint(clip: AudioClip, pos: Vector3, vol: float){
	var temp: GameObject = new GameObject("TempAudio");
	temp.transform.position = pos;
	var audioS = temp.AddComponent(AudioSource);
	audioS.clip = clip;
	audioS.volume = vol;
	audioS.priority = 10; // define a high priority
	audioS.Play();
	Destroy(temp, clip.length);
}

NOTE: Code based on the original PlayClipAtPoint.