I made a script to handle the bgm of the first level of my game for an assessment, it was working fine yesterday, but i haven’t really touched it since yesterday. Now, i do understand the error is telling me that I’m trying to access either [-1] or a placer higher than the array length, but with my current script this makes no sense.
Here is the whole error report:
ArgumentOutOfRangeException: Index is less than 0 or more than or equal to the list count. Parameter name: index
and, Here is my Script,
#pragma strict
@script RequireComponent(AudioSource);
var firstLevelBgm0 : AudioClip;
var firstLevelBgm1 : AudioClip;
var firstLevelBgm2 : AudioClip;
var firstLevelBgm3 : AudioClip;
var firstLevelBgm4 : AudioClip;
var firstLevelBgm5 : AudioClip;
var currentTrack : AudioClip; // Public AudioClip Variable to Easily look at what track is currently playing while testing.
var trackTimer : float = 0.0f; // public Float Varibale to Look at the amount of seconds current track has been playing while testing.
var previousTrack : AudioClip; // Public AudioClip Variable to Look at the track that was previously playing while testing.
private var currentTrackId : int = 0; //
private var previousTrackId : int = 0; //
private var prePreviousTrackId : int = 0; // This variable is there so the algorithm that chooses the next track can avoid the last two tracks.
private var firstLvTrackList = new Array(); //This private Array holds the list of tracks for the first level's bgm.
function Awake()
{
//Fill TrackList at Awake. firstLvTrack[0] is just an empty AudioClip called "None", that will never be played. It is there to make testing nicer
firstLvTrackList[0] = firstLevelBgm0;
firstLvTrackList[1] = firstLevelBgm1;
firstLvTrackList[2] = firstLevelBgm2;
firstLvTrackList[3] = firstLevelBgm3;
firstLvTrackList[4] = firstLevelBgm4;
firstLvTrackList[5] = firstLevelBgm5;
}
function Start () {
currentTrack = firstLevelBgm1; //Set Bgm1 as Current Track at Start.
audio.PlayOneShot(firstLevelBgm1); //Play Bgm1 - Because That is the one I like to start the game with ^~^
}
function Update () {
trackTimer += Time.deltaTime; //Update Time Elapsed
PlayBgm(); //Call PlayBgm to play new Track if needed.
}
function PlayBgm()
{
if (trackTimer > 90) // Change Bgm every 90 seconds.
{
prePreviousTrackId = previousTrackId; //Take Note of the track that played before the previous one.
previousTrackId = currentTrackId; // Take Note of what track was previously playing
previousTrack = currentTrack; // previousTrackId is a public variable, and this allows me to see what was the previous track while testing.
//This process will find and play a new *random* Track, however, it will make sure the previous doesn't again.
currentTrackId = Random.Range(1, 5);
while (currentTrackId == previousTrackId || currentTrackId == prePreviousTrackId) //avoid the last two tracks.
{
Debug.Log("This track already played");
currentTrackId = Random.Range(1, 5);
}
currentTrack = firstLvTrackList[currentTrackId];
audio.Stop(); //Stopprevious Track
audio.PlayOneShot(currentTrack); //Play New Track.
trackTimer = 0; //Reset TrackTimer for new Track.
}
}