A different MusicManager

I did mine differently in the challenge - and it appears to work… I’m just worried there may be other problems with it that I haven’t come across yet.

The question is - Is it ok to continue with my version?

Basically, its based on NOT having a persistent music manager - and instead having it load on every scene. I added some variables in SceneManager to store the current buildIndex - and a getter - which I called from the Start method of the MusicManager…:

public class MusicManager : MonoBehaviour {

public AudioClip[] levelMusicArray;

private AudioSource backgroundMusic;

// Use this for initialization
void Start () {

    backgroundMusic = GetComponent<AudioSource>();
    AudioClip thisLevelMusic = levelMusicArray[LevelManager.GetBuildIndex()];

    if (thisLevelMusic) // If there is some music attached
    {
        backgroundMusic.Stop();
        backgroundMusic.clip = thisLevelMusic;
        backgroundMusic.loop = true;
        backgroundMusic.Play();
    }
}

I thought that as a new scene was loaded, the music manager instance attached to the old one would be destroyed - and its music stop playing. And as the new scene loaded, it the start method in the new instance of MusicManager would run and play the new tune…

Ahh… I think I’ve found a problem with it… Really want the music continuing while moving to the options menu and back - but this is causing it to restart…

Persistent it is then!

1 Like

Yep, without it being persistent you will have very noticeable stops/starts in the music playing.

You could, of course, add some code to perhaps fade out the previous clip, and start a new one on a new scene being loaded. That might be quite nice, especially if it was to differentiate between playable scenes and non-playable scenes, such as the options menu/start scene.