- Home /
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 :/
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 98, 116 and 132 where you change the SongNumber
you probably want to check that you have a number greater or equal to zero, and less than the length of the SongList[]
.
Answer by AlucardJay · Mar 28, 2014 at 02:23 PM
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;
}
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
How to make volume continually go down in C#Script ? 1 Answer
iOS Music Control 0 Answers
How to stop audio in a scene when DontDestroyOnLoad was already called? 1 Answer
Unity and IOS Music Player 0 Answers