I have code to play music for my game. It all works until it tries to reset itself. For some reason Unity crashes whenever it tries to do this.
IEnumerator MusicPlayer()
{
Debug.Log("Start");
if (PlayedAllSongs())
{
Debug.Log("True");
int Repeat = 0;
while (Repeat >= Played.Length)
{
Played[Repeat] = false;
Repeat += 1;
}
}
Debug.Log("Checked");
do
{
Chosen = Random.Range(0, Songs.Length);
Debug.Log(Chosen);
} while (Played[Chosen]);
Debug.Log("Chosen");
Played[Chosen] = true;
Audio.clip = Songs[Chosen];
Audio.Play();
yield return new WaitForSeconds(Audio.clip.length);
Audio.Play();
yield return new WaitForSeconds(Audio.clip.length + 0.3f);
Debug.Log("Finished");
StartCoroutine("MusicPlayer");
}
private bool PlayedAllSongs()
{
Debug.Log("Called");
foreach (bool b in Played)
{
if (!b)
{
return false;
}
}
Debug.Log("true");
return true;
}
Any help would be greatly appreciated, thank you.