Quantcast
Channel: Answers by "KevinCodes4Food"
Viewing all articles
Browse latest Browse all 30

Answer by KevinCodes4Food

$
0
0
Rimrook's solution worked as a baseline in one of our new titles. Thank you RimRook! However, I noticed that if you are using Audio Sources with "Play On Awake" the music may start in the new scene before it can be associated with the singleton and removed, creating a brief two-song at once audio glitch. So I turned off Play on Awake on all background music sources, then updated the code to start playing in the Awake loop only if no other music is playing, or if the clip is different. Also, if the volume is different on the new scene audio source, this has a potential to create issues if an audio clip switch is needed, so my code handles that as well: using UnityEngine; using System.Collections; public class MusicSingleton : MonoBehaviour { private static MusicSingleton instance = null; public static MusicSingleton Instance { get { return instance; } } void Awake() { if (instance != null && instance != this) { audio.Stop(); if(instance.audio.clip != audio.clip) { instance.audio.clip = audio.clip; instance.audio.volume = audio.volume; instance.audio.Play(); } Destroy(this.gameObject); return; } instance = this; audio.Play (); DontDestroyOnLoad(this.gameObject); } }

Viewing all articles
Browse latest Browse all 30

Trending Articles



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