Quantcast
Channel: Questions in topic: "dontdestroyonload"
Viewing all articles
Browse latest Browse all 416

Play different music depending on the current scene

$
0
0
Hello! I have a music player object that has a DontDestroyOnLoad function so that the music plays throughout multiple levels. I want it so that when it gets to a certain scene it changes the music. I've tried putting an if statement in the Awake function that does it, but apparently Awake only gets called once in an object's lifetime. How do I do this? I've tried using OnLevelWasLoaded, but that only seems to get called when I build my game. Also, excuse me if my code is convoluted. I'm a bit of a beginner when it comes to programming in C# in Unity. public AudioClip area1; public AudioClip area2; void Awake() { DontDestroyOnLoad (this.gameObject); if (SceneManager.GetActiveScene ().buildIndex == 9) { AudioSource audio = GetComponent (); audio.clip = area2; audio.Play (); } if (SceneManager.GetActiveScene ().buildIndex > 1 && SceneManager.GetActiveScene ().buildIndex < 7) { AudioSource audio = GetComponent (); audio.clip = area1; audio.Play (); } else { print ("nulling audio clip"); AudioSource audio = GetComponent (); audio.clip = null; audio.Play (); } }

Viewing all articles
Browse latest Browse all 416

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>