thexoy
1
Cannot get my music to change when a boolean changes from false to true. The boolean is definitely changing, but the code itself is not activating the new track. Any ideas what may be causing this?
public class AudioManager : MonoBehaviour
{
public AudioSource BGM;
public AudioClip goTrack;
public bool gameOver = false;
void ChangeBGM(AudioClip music)
{
music = goTrack;
if(gameOver == true)
{
Debug.Log("Playing Game Over Music");
BGM.Stop();
BGM.clip = goTrack;
BGM.Play();
}
}
}
Are you calling the function from another script? It won’t work otherwise.
Or
If you want it to check if the boolean is true or false all the time, put it in the update function.
@thexoy
@thexoy
So the way an audio source work, is that it plays the sound when you want it to.
If you really want it to be successful. Try adding
Audio.stop();
Audiosource.clip = to your clip;
// Add line here
if (audiosource.isPlaying() == false)
{
//Then play you`r tune.
Audiosource.play();
}
Also check in the inspector to see if it is changing??.
Hope this helps
Spip5
3
@thexoy I’d suggest you create a singleton Instance for that AudioManager to get rid of this boolean.
public static AudioManager Instance;
void Awake() {
if (AudioManager.Instance) {
destroy(this);
} else {
Instance = this;
}
and then you call a function tho change your song
public void PlayGameOverSong() {
BGM.Stop();
BGM.clip = goTrack;
BGM.Play();
}
from another script with something like this
void ExternalFunction() {
AudioManager.Instance.PlayGameOverSong();
}
Hope that helps
Edit : To properly answer your question : a function isn’t re-called unless you call it (except for Updates which is called every frame but can be a CPU eater if wrongly used). Changing your bool won’t re-call your function., you would have to change the bool THEN re-call the function. Therefore your bool is not really necessary, just do a function that changes your song (see above).