Hello! This code works, but when the array is empty, as in no elements were added from the inspector, unity enters an infinite loop and the only option is to shut it down. Any ideas? Thanks in advance!
private bool PreviousArrayContainsAudioClipIndex()
{
for (int i = 0; i < previousArray.Length; i++)
{
if (previousArray[i] == audioClipIndex)
{
return true;
}
}
return false;
}
private AudioClip GetClipNullClips()
{
return GetClipFrom(NullClips);
}
private AudioClip GetClipFrom(AudioClip[] clips)
{
if (previousArray == null)
{
previousArray = new int[clips.Length / 2];
}
if (previousArray.Length == 0)
{
// If the the array length is 0 it returns null
return null;
}
else
{
// Psuedo random remembering previous clips to avoid repetition
do
{
audioClipIndex = Random.Range(0, clips.Length);
}
while (PreviousArrayContainsAudioClipIndex());
// Adds the selected array index to the array
previousArray[previousArrayIndex] = audioClipIndex;
// Wrap the index
previousArrayIndex++;
if (previousArrayIndex >= previousArray.Length)
{
previousArrayIndex = 0;
}
}
// Returns the randomly selected clip
return clips[audioClipIndex];
}