Next song problem (music player)

Hi guys, i did this music player, the problem is that if i put for example ten song number, and drag them in song list, when i arrived at the last song (10 for this case) and click on the next song, the script read more than ten songs (it reads infinite songs)
How can i set the song limit at 10? I wrote var SongNumber : int = 10; but don’t work :confused:

var CurrentSong : AudioClip = null;
var SongList : AudioClip[];
var gSkin : GUISkin;
var SongName : String;
var Music : AudioSource;
//per usare lo stex input!
var on : boolean; 
 
 
var SongNumber : int = 10;

var showGUI : boolean = false;


//Volume Audio 
 var hSliderValue : float = 0.0;
 
 
function Start(){

    
    CurrentSong = SongList[SongNumber];
    Music = gameObject.GetComponent(AudioSource);
    Music.loop = false;
    Music.playOnAwake = false;
    Music.clip = CurrentSong;
    Music.volume = 2;
 
audio.Play();
 
NextSong();

}

function Update () {


//Volume Audio 
audio.volume = hSliderValue; 
 
SongName = CurrentSong.name;
 
Music = gameObject.GetComponent(AudioSource);
Music.clip = CurrentSong;
 


if (SongNumber > SongList.length - 1)
{
    SongNumber = 0;
}
if (SongNumber < 0)
{
    SongNumber = SongList.length - 1;
}

}





function OnMouseDown () {
    if (Input.GetMouseButton){
    
    //Per usare lo stex Input!
    on= !on;
    
    if (!on) {
    showGUI = true;
    }
    if (on) {
    showGUI = false;
       
}
}
}



 
function OnGUI(){

if(showGUI){



GUI.skin = gSkin;
 
GUI.Box(Rect(10, Screen.height-125, 420, 130), SongName);


//Volume Audio
hSliderValue = GUI.HorizontalSlider (Rect (180, Screen.height-28, 180, 100), hSliderValue, 0.0, 1.0);

 
if(GUI.Button(Rect(30, Screen.height-41, 40, 40), "<<")){
    SongNumber -= 1;
    CurrentSong = SongList[SongNumber];
    Wait();
}
 
if(audio.isPlaying){
    if(GUI.Button(Rect(75, Screen.height-41, 40, 40), "||")){
       audio.Pause();
    }
}
 
if(!audio.isPlaying){
    if(GUI.Button(Rect(75, Screen.height-41, 40, 40), ">")){
       audio.Play();
    }
}
 
if(GUI.Button(Rect(120, Screen.height-41, 40, 40), ">>")){
    SongNumber += 1;
    CurrentSong = SongList[SongNumber];
    Wait();
}
 
}
}
 
function Wait(){
yield WaitForSeconds(0.05);
audio.Play();
 
}
 
function NextSong(){
yield WaitForSeconds (audio.clip.length);
    SongNumber += 1;
    CurrentSong = SongList[SongNumber];
    Wait();
}

lines 48-55 should be where you increase or decrease the value SongNumber, not in Update. Example :

At line 98 :

SongNumber -= 1;
if (SongNumber < 0)
{
    SongNumber = SongList.length - 1;
}

At line 116 :

SongNumber += 1;
if (SongNumber > SongList.length - 1)
{
    SongNumber = 0;
}