- Home /
Question by
Uio443 · Nov 07, 2018 at 04:59 PM ·
loadingloadlevelmusicload sceneloadlevelasync
Load music only when everything is loaded
I am creating a game which has a lot to do with syncing to the music. Is there any way I can make sure that the music does not load earlier than the level or vice-versa.
Comment
Answer by BlockFade · Nov 07, 2018 at 07:56 PM
Disable Play On Awake on the Music Audio Source and Create a script called "MusicOnSceneLoaded" on an empty game object with the following code.
using UnityEngine;
using UnityEngine.SceneManagement;
public class MusicOnSceneLoaded : MonoBehaviour
{
public AudioSource music;
//Called when Script is loaded;
void OnEnable()
{
music.Stop();
music.playOnAwake = false;
//Plays the music when the scene is loaded.
SceneManager.sceneLoaded += PlayMusic;
}
public void PlayMusic(Scene scene, LoadSceneMode mode)
{
music.Play();
}
}
Comment on this reply if it doesn't work!