Unity Architecture best practice
Add a persistent manager class to the hierarchy. This class should be added to all scenes. Add this class to a scene common prefab. This class will be initiated only once in the complete lifecycle of the application. To achive this persistent behavior add the command "DontDestroyOnLoad(gameObject)" to the Awake() method of the script. The persistent manager is just an empty game object renamed to e.g. PersistentManager with such assigned characteristic script:
using UnityEngine;using UnityEngine.SceneManagement;public class PersistentManagerCode : MonoBehaviour{public int Level;public delegate void GameEvent();public static PersistentManagerCode Instance { get; private set; }public static event GameEvent OnResetGame; // listeners can subscribe to this eventprivate void Awake(){if (Instance == null){Instance = this; // what creates the first instance? The gameobject which is called persistentGameManager - that is all - discover the static vars on topDontDestroyOnLoad(gameObject); // to be persistent, okay - is available in all scenesDoResetGame();}elseDestroy(gameObject);}public void DoResetGame(){if (OnResetGame != null)OnResetGame(); // if there are any subscribers, then call the OnResetGame GameEvent delegate function of the subscriberthis.Level = 0;}...}
to be continued...