Play random sound on NPC when trigger enter

Hi, i’m making a script to play randomly a audio bank i’ve created.

It’s a script attached to a moving NPC wandering in the map.

I want to be able to assign audios clips in the inspector, play a random audio clip on trigger enter, continue sound on trigger exit then stop all the sound if you not in it and when the currently playing audio clip was finished.

If you stay on the trigger, a new sound randomly choosen play in the next 5 seconds.

If you Exit collider and an audio clip is playing and you return in it, the audio clip finish and after 5 seconds play another random sound.

Here my script :

    #pragma strict

  var sound01: AudioClip; var sound02: AudioClip; var sound03: AudioClip; var sound04: AudioClip; var sound05: AudioClip; var sound06: AudioClip; var sound07: AudioClip; var sound08: AudioClip; var sound09: AudioClip; var sound10: AudioClip;


    private var soundArray : AudioClip[];
    private var soundArrayLength : int = 10;

    private var executingSound : boolean = false;
    private var inZoneSound : boolean = false;


    private var soundSource: AudioSource;

    function Start () {
 
        soundSource= gameObject.AddComponent(AudioSource);
        soundSource.loop = false;
        soundSource.volume = 1;

     
        soundArray = new AudioClip[soundArray.Length];
        soundArray[0] = sound01; soundArray[1] = sound02; soundArray[2] = sound03; soundArray[3] = sound04; soundArray[4] = sound05; soundArray[5] = sound06; soundArray[6] = sound07; soundArray[7] = sound08; soundArray[8] = sound09; soundArray[9] = sound10;
    }

    function Update () {
       if (inZoneSound && !executingSound) playRandomSound();
    }

 
    function OnTriggerEnter (other : Collider) {
    if (other.tag == "Player"){
       inZoneSound = true;   
    }
    }

  
    function OnTriggerExit (other : Collider) {
    if (other.tag == "Player"){
       inZoneSound = false;   
    }
    }

    function playRandomSound () {
   
        executingSound = true;
       
     
        var randomSound = Random.Range(0,soundArray.Length);
        soundSource.PlayOneShot(soundArray[randomSound]);
       
     
        yield WaitForSeconds(soundSource.clip.length);

        
        yield WaitForSeconds(5);
        executingSound = false;
    }

But he doesn’t work
Th clip play at start game and never stop ! He make a random but in continue without the trigger collision !

I’m french, sorry for my english !
Any ideas ? ^^

Ok, I figured out with some help.

Here the solution :

#pragma strict
 
var sound01: AudioClip; var sound02: AudioClip; var sound03: AudioClip; var sound04: AudioClip; var sound05: AudioClip; var sound06: AudioClip; var sound07: AudioClip; var sound08: AudioClip;var sound09: AudioClip;var sound10: AudioClip;
 
 
private var soundArray : AudioClip[];
private var soundArrayLength : int = 10;
 
private var executingSound : boolean = false;
private var inZoneSound : boolean = false;
 
private var soundSource: AudioSource;
 
function Start () {
 
soundSource= gameObject.AddComponent(AudioSource);
soundSource.loop = false;
soundSource.volume = 1;
 
 
soundArray = new AudioClip[8];
soundArray[0] = sound01; soundArray[1] = sound02; soundArray[2] = sound03; soundArray[3] = sound04; soundArray[4] = sound05; soundArray[5] = sound06; soundArray[6] = sound07; soundArray[7] = sound08;soundArray[8] = sound09;soundArray[9] = sound10;
}
 
function Update () {
if (inZoneSound && !executingSound) playRandomSound();
}
 
 
function OnTriggerEnter (other : Collider) {
if (other.tag == "Player"){
inZoneSound = true;
}
}
 
 
function OnTriggerExit (other : Collider) {
if (other.tag == "Player"){
inZoneSound = false;
}
}
 
function playRandomSound () {
 
executingSound = true;
 
 
var randomSound = Random.Range(0,soundArray.Length);

	var audioClipTemp = soundArray[randomSound];
soundSource.PlayOneShot(soundArray[randomSound]);
//Debug.Log("audio source length " + audioClipTemp.length);


yield WaitForSeconds(audioClipTemp.length);
yield WaitForSeconds(5);
 
 
executingSound = false;
}

Thanks anyway :wink: