问题:
退出游戏(停止运行Unity工程)时报错:Some objects were not cleaned up when closing the scene. (Did you spawn new GameObjects from OnDestroy?)
The following scene GameObjects were found:******
原因:
在A脚本(单例)中定义了事件,在B脚本中注册了A的事件,退出游戏时物体的销毁是随机的,A在B之前被销毁,B在销毁前调用OnDestroy(),此时A已经被销毁,作为单例被调用时重新创建了新物体,因此发生报错:在OnDestroy中创建了物体
解决:
试着修改脚本的执行顺序,不过没解决报错···不可行
(设置脚本执行顺序,好像只能影响Awake/OnEnable/Update函数的调用顺序···)
根据参考,在单例中增加判断,虽然会解决这个报错,但是会生成新的报错,因为A脚本被销毁后,单例返回空值,在运行到B脚本的OnDestroy时,报错:A的单例为空,因此这样修改也不可行
最后:在想到合理的解决办法之前,emmmmm,因为是在退出游戏时的报错,可以暂时忽略,不影响正常游戏体验······