- Home /
Help : Playing Multiple Background Musics !
I need help in playing multiple background musics, i have 3 music clip, i attached them to 3 different objects in my scene, i have one audio mangaer script in my scene which works like this,
var ambient1 : GameObject;
var ambient2 : GameObject;
var ambient3 : GameObject;
function Start () {
ambient1.audio.Play();
}
function Update () {
if(!ambient1.audio.isPlaying)
{
ambient2.audio.Play();
if(!ambient2.audio.isPlaying)
{
ambient3.audio.Play();
if(!ambient3.audio.isPlaying)
{
ambient1.audio.Play();
}
}
}
}
but it only plays first ambient, it does not play anyother ambient... is there something i am missing? and also, how can i add a delay of 2 min between them?
are your music is loping i mean first ambient keep playing
Answer by _Yash_ · Oct 23, 2014 at 09:50 AM
It actually plays second but it keeps starting second track again n again so it feels like nothing is playing after first track.
You can make 3 coroutines for playing each track and add a delay according to clips lengh.
Something like
void Start(){
StartCoroutine(amb1());
}
Ienumerator amb1(){
//play track 1
//add delay, yield return new waitForSeconds(t1time);
StartCoroutine(amb2());
}
Ienumerator amb2(){
//play track 2
//add delay, yield return new waitForSeconds(t2time);
StartCoroutine(amb3());
}
Ienumerator amb3(){
//play track 3
//add delay, yield return new waitForSeconds(t3time);
StartCoroutine(amb1());
}
@Yash oh i get it, what you said, but ins$$anonymous$$d of using co-routine(since i dont know much about them), i used boolean variables and AudioClip Ins$$anonymous$$d of GameObject, and it works.. i added the delay with audio.PlayDelayed()
var ambient1 : AudioClip;
var ambient2 : AudioClip;
var ambient3 : AudioClip;
var playedFirst : boolean = false;
var playedSecond : boolean = false;
var playedThird : boolean = false;
function Start () {
audio.clip = ambient1;
audio.Play();
playedFirst = true;
playedSecond = false;
}
function Update () {
if(!audio.isPlaying)
{
if(!playedFirst)
{
audio.clip = ambient1;
playedFirst = true;
playedSecond = false;
audio.PlayDelayed(120);
}
else if(!playedSecond)
{
audio.clip = ambient2;
playedSecond = true;
playedThird = false;
audio.PlayDelayed(120);
}
else if(!playedThird)
{
playedThird = true;
playedFirst = false;
audio.clip = ambient3;
audio.PlayDelayed(120);
}
}
}
This also worked.
Your answer
Follow this Question
Related Questions
How to make music play always without duplicates? 0 Answers
Can't get my sound to work.. 1 Answer
Getting my 2D sound trigger to play only once 1 Answer
Music play when MOVING.. 2 Answers