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…