Audio scripting

Is there a way to have an audiosource play on on a loop, but have a delay of a certain time inbetween each loop?

The best option would be to add a silent sound before (or after) the sound inside the wav or mp3 file. The other way to do it would be to add this script to your audio object (Haven’t tried it yet but it should do the work). Make sure your audioSource is NOT set on loop though.

public float delay;
bool calledCoroutine;

void Update(){
        if(!GetComponent<AudioSource>().isPlaying && !calledCoroutine){
              StartCoroutine(PlaySound());
              calledCoroutine = true;
        }
}

IEnumerator PlaySound(){
         yield return new WaitForSeconds(delay);
         GetComponent<AudioSource>().Play();
         calledCoroutine = false;
}

Hi there, literally, no, you need to evaluate the clip so you can get if the clip is still playing or not, set a proper delay and replay the clip, something like this:

AudioSource source; // Without loop

if (source.isPlaying) {
    // wait until finished;
}
else {
    // wait for a delay timer;
    // play again;
    source.Play();
}

Or something like this:

float timer = source.clip.length; // The clip time length

source.Play(); // With loop

yield return new WaitForSeconds(timer);
source.Pause();
yield return new WaitForSeconds(delay);
source.Play();
// Then repeat...

Hope this helps, cheers.