Change music when enemy is in scene
I have this proble, i want that when the game object tagged "Boss" spawns, the music changes, but when the boss is killed the music go back to normal. i´ve tried lot of things, i need help. I use a game object to controll the music, and 2 Audio sources.
this is my code: public AudioSource _AudioSource1; public AudioSource _AudioSource2;
GameObject boss;
bool bossAlive = false;
bool bossDeath = false;
void Start()
{
boss = GameObject.FindGameObjectWithTag("Boss");
_AudioSource1.Play();
}
void Update()
{
if(boss)
{
bossAlive = true;
bossDeath = false;
}
if (!boss)
{
bossAlive = false;
bossDeath = true;
}
if (bossAlive)
{
if (_AudioSource1.isPlaying)
{
_AudioSource1.Stop();
_AudioSource2.Play();
}
bossAlive = false;
}
if (bossDeath)
{
if (_AudioSource2.isPlaying)
{
_AudioSource2.Stop();
_AudioSource1.Play();
}
bossDeath = false;
}
}
Comment
Your answer
Follow this Question
Related Questions
Find closest object with a certain tag 2 Answers
Usage of multiple tags or some other workaround? 1 Answer
Multiple tags for one GameObject 3 Answers
i don't know why tag option is dosen't work 0 Answers
Tag Editor is all grayed out 0 Answers