- Home /
Question by
FatalNightmare · Nov 10, 2013 at 04:52 AM ·
javascriptaudioloopradio
Audio not playing (Radio station-like)
Hi guys! I am trying to make a boom box or stereo so that when the mouse is hovered over the stereo and press 'M' the first song in the list plays, then if they press 'M' again the next song in the list plays. I eventually want it to loop (haven't done the looping yet) but when I try out my code, there are no errors yet the song isn't playing and track song # isn't adding 1. Here is my code, thanks for any help.
public var JustDrive : AudioClip;
public var Song2 : AudioClip;
public var Song3 : AudioClip;
public var Song4 : AudioClip;
public var songcounter : int = 0;
function OnMouseEnter () {
if(Input.GetKey(KeyCode.M)){
if(songcounter == 0){
audio.PlayOneShot(JustDrive);
songcounter += 1;
}
if(songcounter == 1){
audio.PlayOneShot(JustDrive);
songcounter += 1;
}
if(songcounter == 3){
audio.PlayOneShot(Song3);
songcounter += 1;
}
if(songcounter == 4){
audio.PlayOneShot(Song4);
songcounter += 1;
}
}
}
Comment
Best Answer
Answer by deltamish · Nov 10, 2013 at 05:01 AM
Hi That is really simple problem The thing is you have defined the function called OnMouseEnter but it never gets called .It only gets called only when mouse enters a GUI Element so to fix the problem
remove the OnMouseEnter function and replace it with this code
function Update()
{
PlayAudio();
}
function PlayAudio()
{
if(Input.GetKey(KeyCode.M)){
if(songcounter == 0){
audio.PlayOneShot(JustDrive);
songcounter += 1;
}
if(songcounter == 1){
audio.PlayOneShot(JustDrive);
songcounter += 1;
}
if(songcounter == 3){
audio.PlayOneShot(Song3);
songcounter += 1;
}
if(songcounter == 4){
audio.PlayOneShot(Song4);
songcounter += 1;
}
}
}
}