// Update is called once per frame void OnSceneLoaded(Scene scene, LoadSceneMode mode) { if (startindex != SceneManager.GetActiveScene().buildIndex) { instance = null; SceneManager.sceneLoaded -= OnSceneLoaded; Destroy(gameObject); } }
// Start is called before the first frame update void Start() { if (!instance) { instance = this; SceneManager.sceneLoaded += OnSceneLoaded; startindex = SceneManager.GetActiveScene().buildIndex; DontDestroyOnLoad(gameObject); } else if (instance != this) { Destroy(gameObject); } }