Some objects were not cleaned up when closing the scene. (Did you spawn new GameObjects from OnDestr

Posted weiqiangwaideshijie

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Some objects were not cleaned up when closing the scene. (Did you spawn new GameObjects from OnDestr相关的知识,希望对你有一定的参考价值。

项目在切换场景或者是关闭Unity运行界面的时候,出现Some objects were not cleaned up when closing the scene. (Did you spawn new GameObjects from OnDestroy?)错误。原因是我在一个不销毁的单利里面,定义了一个事件,在外部去注册这个事件。

        /// <summary>
        /// 数据改变通知的事件
        /// </summary>
        public GameDataDel GameDataHandle;
       public static SaveGameData _instance;
        public static SaveGameData Instance
        {
            get
            {
                if (_applicationIsQuitting)
                {
                    return null;
                }
                if (_instance == null)
                {
                    GameObject go = new GameObject("SaveGameData");
                    SaveGameData saveGameData = go.AddComponent<SaveGameData>();
                    DontDestroyOnLoad(go);
                    _instance = saveGameData;
                }
                return _instance;
            }
        }

外面事件的注册

        private void OnEnable()
        {
            Ctr_PlayerInfo.OnShowLifeNumEve += OnFreshLifeNum;
            SaveGameData.Instance.GameDataHandle += OnFresh;
        }

        private void OnDisable()
        {
            if (Ctr_PlayerInfo.OnShowLifeNumEve != null)
            {
                Ctr_PlayerInfo.OnShowLifeNumEve -= OnFreshLifeNum;
            }
            if (SaveGameData.Instance!= null)
            {
                SaveGameData.Instance.GameDataHandle -= OnFresh;
            }

        }

当我退出时,应用程序对象会以随机顺序被销毁。因此,如果gameManager实例在任何OnDisable()方法之前被销毁,那么在应用程序退出导致错误后,将创建gameManager类的新实例!!为了解决这个错误,我做了以下修改:

      public static SaveGameData _instance;
        private static bool _applicationIsQuitting = false;
        public static SaveGameData Instance
        {
            get
            {
                if (_applicationIsQuitting)
                {
                    return null;
                }
                if (_instance == null)
                {
                    GameObject go = new GameObject("SaveGameData");
                    SaveGameData saveGameData = go.AddComponent<SaveGameData>();
                    DontDestroyOnLoad(go);
                    _instance = saveGameData;
                }
                return _instance;
            }
        }
        public void OnDestroy()
        {
            _applicationIsQuitting = true;
        }

  

以上是关于Some objects were not cleaned up when closing the scene. (Did you spawn new GameObjects from OnDestr的主要内容,如果未能解决你的问题,请参考以下文章

rsync error: some files/attrs were not transferred (see previous errors) (code 23)

升级IDEA时出现Some conflicts were found in the installation area

升级IDEA时出现Some conflicts were found in the installation area

Android Studio Update Faile : some conflicts were found in the installation area

Android Studio Update Faile : some conflicts were found in the installation area

错误记录Kotlin 报错 ( Some kotlin libraries attached to this project were compiled with a newer kotlin )