How to change priority of audio clips

Hi,

im creating a scene with 128 recorded ambient sounds. Each audio source for a different position in the scene. But I´m exceeding the play limit of 32 simultaneous audio files. It does´n work to use the collide function for play the sounds because the ambient sounds must be played simultaneously to stay synchronized.
Can I write a script to change the priority of each audio clip while collide to each game object?

If these sounds are 3D, I suspect that you don’t have to worry about this: according to the docs, sounds that are less audible (the farthest ones) are occluded first when there are more AudioSources than available mixer inputs (priority is also taken into account).

Anyway, I suppose that you could use a trigger to raise/lower the priority of each sound source: add or child a sphere trigger of the desired radius (range) to the player, and add also a kinematic rigidbody to it (moving triggers require a rigidbody, even kinematic), then place this code in every AudioSource:

function OnTriggerEnter(other: Collider){
  // audio source inside player range:
  audio.priority = 100; // set a higher priority
}

function OnTriggerExit(other: Collider){
  // audio source out of player range:
  audio.priority = 128; // restore default priority
}

NOTE: Every object having one of these AudioSources must also have a collider in order to be detected by the trigger (rigidbody not required).

Hi, with this script I can create audio zones with a random form that are on while the rest of my audio sources are turned off. So I could reduced the numbers of audio sources playing simultaneously. Thanks to Daniel Murcia!

For use it, create a surface with the form you need and place it on the floor level, add a mesh collider, select-on the is trigger option, unselect the mesh render option and attach to the surface you have created the audio sources you want to be played simultaneously when enter in the zone designed by your surface.

So you can differentiate audio zones that are played at once when they are triggered.

#pragma strict
 
var audioSources;
 
function Start(){
    audioSources = gameObject.GetComponentsInChildren(AudioSource);
}
function OnTriggerExit(){
    Debug.Log("stopping sounds");
    Debug.Log(this);
    var self = this;
    for (var source:AudioSource in audioSources) {
        Debug.Log(source);
        source.Stop();
    }
}
 
function OnTriggerEnter(){
    Debug.Log("starting sounds");
    Debug.Log(this);
    var self = this;
    for (var source:AudioSource in audioSources) {
        Debug.Log(source);
        source.Play();
    }
}