[ANSWERD] Sounds Loop with delay

Hello!

I am working on a script that plays ambient sounds with a 10 second delay but I don’t know how I get the code to loop. Can someone please help me with my script to make the code to play sounds loop?

My code: (I know it is wrong but this was the closest it could come)

var sounds : AudioClip[];
var timeBetweenSounds = 10.0;

function Start () {
	
	yield WaitForSeconds(timeBetweenSounds);
	audio.clip = sounds[ Random.Range( 0, sounds.Length ) ];
	audio.Play();
}

Thanks. :slight_smile:

Create and call a new function and loop it.

    var sounds : AudioClip[];
    var timeBetweenSounds = 10.0;
    var timesToPlay : int = 20;
     
    function Start () {
        SoundLoop();  
    }

//If you want to play it specific amount if times
    function SoundLoop(){
        for(int i = 1; i <= timesToPlay; i++){
            yield WaitForSeconds(timeBetweenSounds);
            audio.clip = sounds[ Random.Range( 0, sounds.Length ) ];
            audio.Play();
        }
    }

//If you want to play it forever
    function SoundLoop(){
        while(true){ //or for(;;){
            yield WaitForSeconds(timeBetweenSounds);
            audio.clip = sounds[ Random.Range( 0, sounds.Length ) ];
            audio.Play();
        }
    }