Hey I want my music player to randomly select songs at all times - NOT just pick a random start song and then play the rest in a predefined order. The script I wrote is just not doing the job and I can’t figure out why. I would REALLY appreciate some assistance.
I have an array that holds ID numbers to each song and after each song is played the ID is removed from the array and then a new random number is picked based on the new length of the array. But it’s not consistent. Can someone PLEASE advise on what I am not understanding?
@script RequireComponent (AudioSource)
var musicTrack : AudioClip[];
private var songID : int = 0;
private var idArray : Array = new Array();
function Start(){
DontDestroyOnLoad(this);
audio.loop = false;
initIdArray();
songID = idArray[Random.Range(0,idArray.length)];
PlayMusic();
}
function PlayMusic(){
while (true){
audio.clip = musicTrack[songID];
audio.Play();
if(audio.clip) {
yield WaitForSeconds(audio.clip.length);
idArray.RemoveAt(songID);
if(idArray.Length == 0){
initIdArray();
}
songID = idArray[Random.Range(0,idArray.length)];
}
}
}
function initIdArray(){
for(var m : int = 0; m < musicTrack.length; m++){
idArray.Add(m);
}
}