public IEnumerator PlayNextInQueue(float delay, bool mustPlayIndependent) { yield return(new WaitForSecondsRealtime(delay)); if (audioQueue.Count > 0) { isPlaying = true; if (mustPlayIndependent) { int myID = Random.Range(int.MinValue, int.MaxValue); playOrder.Enqueue(myID); //wait until its my turn to play yield return(new WaitUntil(() => playOrder.Peek() == myID)); } //last in queue if (audioQueue.Count == 1) { source.loop = loopLast; } source.clip = audioQueue[0].clip; if (audioQueue[0].subtitle != null) { game.StartCoroutine(game.DisplaySubtitles(audioQueue[0].subtitle.text, audioQueue[0].clip.length)); } source.Play(); yield return(new WaitForSecondsRealtime(0.1f)); yield return(new WaitWhile(() => source.isPlaying)); if (audioQueue.Count > 0) { audioQueue.RemoveAt(0); } //finished queue finishedQueue = audioQueue.Count == 0; if (mustPlayIndependent) { playOrder.Dequeue(); } isPlaying = false; if (autoPlayQueue) { StartCoroutine(PlayNextInQueue(0, mustPlayIndependent)); } } }