- Home /
Audio loop half working
hey, i've been working on my own music loop to play throughout the game. the control is on a "static" object, meaning it never gets destroyed on awake.
So what i want is a pause menu, and it has two buttons, a next and back button. which will change to the next, or last song. and i got that sorta working. but what's supposed to happen is that no matter how many times you press either button consecutively, it will loop through the array of songs. and it works for the back button. but the next button, after the last song in hte array, it ALWAYS says that "IndexOutOfRangeException: Array index is out of range."
caould you please look through my code and help me figure out what's wrong?
Thanks you!
static var Paused : boolean = false; var soundTrack : AudioClip[]; var myFont : Font; var currentTrack = 0; var e : Vector4[]; var TrackID : int; private var dir : int = 0; private var timer : float; private var timeStarted = false; private var notInput = true;
//awake fn function Awake () { dir = 0; // Make this game object and all its transform children // survive when loading a new scene. DontDestroyOnLoad (transform.gameObject); }
//GUI fn function OnGUI() { if(Paused) { GUI.skin.box.font = myFont; GUI.Box(Rect((Screen.width / 2) - (180), (Screen.height / 2) - (260),(Screen.width / 4)+(180), (Screen.height / 4)-(100)),"Your game is paused, press (r) to resume.");
if(GUI.Button(Rect((Screen.width / 2) + e[0].x, (Screen.height / 2) + e[0].y, (Screen.width / 4) + e[0].z, (Screen.height / 4) + e[0].w), "Next Song>>")) { dir = 1; notInput = false; CheckID(); } if(GUI.Button(Rect((Screen.width / 2) + e[1].x, (Screen.height / 2) + e[1].y, (Screen.width / 4) + e[1].z, (Screen.height / 4) + e[1].w), "<<Last Song")) { dir = -1; notInput = false; CheckID(); } } }
//my fn function CheckID() { switch(dir) { case -1: switch (TrackID) { case 0: notInput = true; dir = 0; break; case 1: currentTrack = soundTrack.length -1; TrackID = 4; break; case 2: currentTrack--; TrackID = 4; break; case 3: currentTrack--; TrackID = 4; break; case 4: TrackID = 0; break; } break; case 1: switch (TrackID) { case 0: notInput = true; dir = 0; break; case 1: currentTrack++; TrackID = 4; break; case 2: currentTrack++; TrackID = 4; break; case 3: if(currentTrack >= soundTrack.length -2) { currentTrack = 0; } currentTrack = 0; TrackID = 4; break; case 4: TrackID = 0; break; } break; } }
//update fn function Update () { if(audio.clip != soundTrack[currentTrack]) { audio.Stop(); audio.clip = soundTrack[currentTrack]; audio.Play(); } var minutes : float = Mathf.Floor(timer / 60); var seconds : float = Mathf.RoundToInt(timer%60);
if(Input.GetKeyDown("p")) { Paused = true; } if(Input.GetKeyDown("r")) { Paused = false; } if(currentTrack >= soundTrack.length -2) { TrackID = 3; } if(currentTrack <= 0) { TrackID = 1; } if(currentTrack > 0 && currentTrack < soundTrack.length) { TrackID = 2; }
if(!audio.isPlaying) { if(minutes > 1 || seconds > 120) { dir = 1; CheckID(); } audio.clip = soundTrack[currentTrack]; audio.Play(); } if(timeStarted && notInput) { timer += Time.deltaTime; } if(timeStarted && !notInput) { timer = 0; } }
does the variable currentTrack gets bigger than the size of the array of soundTrack?? maybe could be that
yes, i tried logging what currentTrack was. and i started with only 5 items in the array (0 through 4) but currentTrack does register as 5, which would put it over the top, but how do i fix it? i tried adjusting what made trackId = 3 (to set it to zero ins$$anonymous$$d of 5), but that didnt do anything..... so im not sure how to fix it....
never$$anonymous$$d... i fixed it, where i said to make it normally go to the next song, the factors for that to happen where to inclusive. so ins$$anonymous$$d of "currentTrack > 0 && currentTrack < soundTrack.length", i changed it to "currentTrack > 0 && currentTrack < soundTrack.length - 1" and that's all i needed. thanks for looking at it for me though!
Your answer
![](https://koobas.hobune.stream/wayback/20220613110312im_/https://answers.unity.com/themes/thub/images/avi.jpg)