Scripting issue implimenting a weighting or 'chance audio will play' feature

Hey,

I’m fairly new to scripting wanted to know if anyone would mind helping me out. I have been attempting to expand the functionality of the AudioSource component in javascript to include

Randomised play from selection of samples
Chance the SFX will play in percentage
Randomised pitch between ranges
Randomised volume between ranges
Voice limiter

So far I’ve been able to able to implement everything but the voice limiting and the ‘weighting’ or probability a SFX will play.

Here is my script so far, would anyone be able to point me in the right direction?

#pragma strict

var SFX : AudioClip[ ];

var MinPitchVariation = 0.9f;
var MaxPitchVariation = 1.1f;

var MinVolumeVariation = 0.9f;
var MaxVolumeVariation = 1.1f;

function Update () {
if(Input.GetButtonUp(“Jump”))
{

//Random play from Array
GetComponent.().clip = SFX[Random.Range(0, SFX.Length)];

//Random Volume
GetComponent.().volume = Random.Range (MinVolumeVariation, MaxVolumeVariation);

// Random Pitch
GetComponent.().pitch = Random.Range(MinPitchVariation, MaxPitchVariation);

//Chance SFX will Play (Default to 100% chance)

//Voice Limiting

//Play SFX
GetComponent.().Play();

}
}

What do you try to achieve with “Voice Limiting”? I think you should be able to implement that using the Priority setting of your AudioSources and the Max Voice Settings of your project (so Unity takes care of it).

As far as weighting goes: check this [1]

[1] Weighted/Biased Random Number Generation with JavaScript based on Probability - Code Theory

I believe he wants to cap the number of each sound, not the total.

The voice limiting will probably require refactoring the code so it keeps track of all like voices, in a single script. It shouldn’t be done in a script that appears multiple places.