持续创作,加速成长!这是我参与「掘金日新计划 · 6月更文挑战」的第5天,点击查看活动详情
遇到问题:
在开发过程中遇到的报错原文:
MissingReferenceException: The object of type 'GameObject' has been destroyed but you are still trying to access it.Your script should either check if it is null or you should not destroy the object.
报错翻译:
MissingReferenceException:类型为“GameObject”的对象已被销毁,但您仍试图访问它。 您的脚本应该检查它是否为空,或者不应该销毁对象。
问题分析
其实看到这两句话的描述,这个错误就很明显了。
最大的误区:就是在销毁(一般是做了限定写的类似Destory的方法)了,你认为不用的“GameObject的对象,,而你的代码里面却还在调用,,,就会出现这样的问题,
报错原文
报错全文:
MissingReferenceException: The object of type 'GameObject' has been destroyed but you are still trying to access it. Your script should either check if it is null or you should not destroy the object. //下面是报错详情...和具体项目代码有关系... RoundOverView.AllPlayerInfos () (at Assets/Scripts/Wang/View/RoundOverView.cs:145) RoundOverMediator.RoundOver (IEvent evt) (at Assets/Scripts/Wang/View/RoundOverMediator.cs:76) strange.extensions.dispatcher.eventdispatcher.impl.EventDispatcher.invokeEventCallback (System.Object data, strange.extensions.dispatcher.eventdispatcher.api.EventCallback callback) (at Assets/Scripts/StrangeIOC/strange/extensions/dispatcher/eventdispatcher/impl/EventDispatcher.cs:208) strange.extensions.dispatcher.eventdispatcher.impl.EventDispatcher.Dispatch (System.Object eventType, System.Object data) (at Assets/Scripts/StrangeIOC/strange/extensions/dispatcher/eventdispatcher/impl/EventDispatcher.cs:153) EQWControllerMediator+<Ro_Ys>c__Iterator2.MoveNext () (at Assets/Scripts/ErQiWang/Controller/EQWControllerMediator.cs:403) UnityEngine.SetupCoroutine.InvokeMoveNext (IEnumerator enumerator, IntPtr returnValueAddress) (at C:/buildslave/unity/build/Runtime/Export/Coroutines.cs:17)
解决问题
此类问题不是很常见,因为开发过程中基本不会犯这种错误。
我记得当时应该是,在类似于(OnDisable)销毁时调用的方法里面使用了要销毁的游戏对象,其实应该是要在这个方法里面取消调用,复制过来的代码没有进行详细查看,导致出错,自己还看了半天,才看到是什么问题(很蠢a),以此记录一下犯过的错,避免以后再犯。
若你也遇到了同样的问题,建议你可以根据这个报错中提示的物体名称,去查找其生命期,即:在何处生成,在何处销毁等逻辑。重新按照执行逻辑梳理一次。