using System.Linq; using UnityEngine; namespace Crosstales.Common.Util { /// Allows any Unity gameobject to survive a scene switch. This is especially useful to keep the music playing while loading a new scene. [DisallowMultipleComponent] public class SurviveSceneSwitch : Singleton { #region Variables ///Objects which have to survive a scene switch. [Tooltip("Objects which have to survive a scene switch.")] public GameObject[] Survivors; //any object, like a RadioPlayer private const float ensureParentTime = 1.5f; private float ensureParentTimer; private Transform tf; #endregion #region MonoBehaviour methods private void Start() { ensureParentTimer = ensureParentTime; tf = transform; } private void Update() { ensureParentTimer += Time.deltaTime; if (Survivors != null && ensureParentTimer > ensureParentTime) { ensureParentTimer = 0f; foreach (GameObject _go in Survivors.Where(_go => _go != null)) { _go.transform.SetParent(tf); } } } #endregion } } // © 2016-2020 crosstales LLC (https://www.crosstales.com)