(Sorry for my english)
Hi. I’ve made a coroutine that make a sound loop. When the audio clip is finished, the coroutine take another one and play it. I don’t know what did I make wrong, but the coroutine play one song and doesn’t play a second sound at the end. Here is the script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class musiqueFond : MonoBehaviour {
// Audio source
private AudioSource source;
// Sounds
[SerializeField]
private AudioClip[] musiques = new AudioClip[9];
// Random number
private int aleatoire;
// Get component and start coroutine
void Start() {
source = GetComponent<AudioSource>();
StartCoroutine("boucleSon");
}
// Coroutine
IEnumerator boucleSon() {
aleatoire = Random.Range(0, 9);
source.PlayOneShot(musiques[aleatoire]);
yield return new WaitForSeconds(musiques[aleatoire].length + 2f);
}
}
I’m pretty sure that I make a big mistake.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class musiqueFond : MonoBehaviour {
// Audio source
private AudioSource source;
// Sounds
[SerializeField]
private AudioClip[] musiques = new AudioClip[9];
// Get component and start coroutine
void Start() {
source = GetComponent<AudioSource>();
StartCoroutine( boucleSon() );
}
// Coroutine
IEnumerator boucleSon() {
while( true )
{
int aleatoire = Random.Range(0, musiques.Length);
source.PlayOneShot(musiques[aleatoire]);
yield return new WaitForSeconds(musiques[aleatoire].length + 2f);
}
}
}