Upcoming change to unity regarding on level was loaded

Hello restlessdan.

Thx for the info and it worked.
Had this deprecated-message just in GlitchGarden-Project now, and I dont like red-messages or messages with exclamation-marks in console while running a programm.
Yes your code works.
Maybe its important to say, that SceneManager_sceneLoaded() method is called before the Start() method.
Important, because we did some initialisation-stuff in the MusicManager in Glitch Garden.

void Start(){
		audioSource = GetComponent<AudioSource> ();
	}

But this is called after the “Level loaded”.
So the init of the audioSource needs to be put to Awake() or OnEnable().
(Its also possible to put a OnEnable() and a On Disable() method in between, which is automatically called after Awake() and before Scenemanager_sceneLoaded.)

I think the sequence of these new functions is:
Awake()->OnEnable()->SceneLoaded()*->Start()

So this is how I implemented my new MusicManager in GlitchGarden:

using UnityEngine;
using System.Collections;
using UnityEngine.SceneManagement;


public class MusicManager : MonoBehaviour {

	public AudioClip[] levelMusicChangeArray;
	private AudioSource audioSource;
	// Use this for initialization
	void Awake () {
		DontDestroyOnLoad (gameObject);
		Debug.Log (name + ":Awake()");

	}

	/*OLD CODE OnLevelWasLoaded deprecated

	void Start(){
		audioSource = GetComponent<AudioSource> ();
	}

	void OnLevelWasLoaded(int level){
		AudioClip actualLevelMusic = levelMusicChangeArray [level];
		Debug.Log (name + ":OnLevelWasLoaded is playing " + actualLevelMusic);
		audioSource.clip = actualLevelMusic;
		audioSource.loop = true;
		audioSource.Play ();
	}
	*/

	void OnEnable(){
		Debug.Log (name + ":OnEnable()");
		audioSource = GetComponent<AudioSource> ();
		SceneManager.sceneLoaded += OnLevelFinishedLoading;
	}


	void OnDisable(){
		SceneManager.sceneLoaded -= OnLevelFinishedLoading;
	}

	void OnLevelFinishedLoading(Scene scene,LoadSceneMode mode){
		int level = scene.buildIndex;
		AudioClip actualLevelMusic = levelMusicChangeArray [level];
		Debug.Log (scene.name + ":OnLevelFinishedLoading is playing " + actualLevelMusic);
		audioSource.clip = actualLevelMusic;
		audioSource.loop = true;
		audioSource.Play ();
	}
}
2 Likes