How do I play an array of audio in turn in Unity?

Viewed 33

I have a fairly large array of 60 tracks. I need them to play one by one. When the scene changes, the track is also interrupted and the next track is played in turn. That is, in one scene can play 0, 1, 2 items. After switching scenes, 3 should play, and so on. On the Internet I found a function that seems to work the way I want. But I do not quite understand how to call function correctly.

public AudioClip[] clipArray;
public AudioSource effectSource;
private int clipIndex;

void PlayRoundRobin() {

if (clipIndex < clipArray.Length)
{
effectSource.PlayOneShot(clipArray[clipIndex]);
clipIndex++;
}

else
{
clipIndex = 0;
effectSource.PlayOneShot(clipArray[clipIndex]);
clipIndex++;
}
1 Answers

Put your method into your scene manager. If you load the scene, call the method. After that, call the method whenever you want to play the next track.

Related