|
Post by picklesiidx on Aug 20, 2014 8:14:44 GMT
I have a unique Master Audio prefab in each of my scenes. When each scene starts up, I'd like to set the Master Audio's volume levels based on stored options I have, and then play the scene's music. The issue is, if I call this too early, then I'll get a null reference error because it's pointing to the previous scene's Master Audio instance as that's still being cleaned up (I think).
My thought was to run a coroutine on Awake that waits for MasterAudio.Instance to no longer be null, but this doesn't work, as it references the last scene's MasterAudio. Is there any callback to when the Master Audio Instance is ready and I can play a sound?
|
|
|
Post by DarkTonic Dev on Aug 20, 2014 16:19:19 GMT
Just use Start event. Awake is too early because Master Audio is initializing during Awake.
|
|
|
Post by picklesiidx on Aug 20, 2014 17:00:37 GMT
Got it, with the current version. Here's my coroutine to wait for the Master Audio to be set:
IEnumerator WaitForMasterAudio(){ if(MasterAudio.Instance.audio == null){ yield return 0; } // now you can do things that require playing sound
// If you want to change a playlist, there is another check you need to do: foreach(MasterAudio.Playlist playlist in MasterAudio.Instance.musicPlaylists){ if(playlist.playlistName == "name of your playlist in this scene"){ MasterAudio.ChangePlaylistByName("name of your playlist in this scene", false); } else { yield return 0; } } }
|
|
|
Post by DarkTonic Dev on Aug 20, 2014 23:02:05 GMT
That's not correct. MasterAudio.Instance doesn't always have an audio component (one is used for preview purposes only). But anyway that wouldn't guarantee that Awake code in Master Audio had finished. It may work sometimes and not all the time. As I said, just do your code in the Start event and it will work. Awake is too early and won't always work. No reason to have to use a CoRoutine.
|
|
|
Post by picklesiidx on Aug 21, 2014 18:01:11 GMT
Sounds good. Thanks for following up!
|
|
|
Post by DarkTonic Dev on Aug 21, 2014 22:28:44 GMT
No problem.
|
|