The object of type 'RectTransform' has been destroyed but you are still trying to access it

MissingReferenceException: The object of type 'RectTransform' 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.

bl_HUDText.OnGUI () (at Assets/UHUDText/Content/Script/Core/bl_HUDText.cs:80)

这种情况是使用UGUI的HUD时,跳转了场景,hud要删除创建出来的text相关,却找不到这个东西,所以就报空了。

HUD的相关代码是这样的

    /// 
    /// Disable all text when this script gets disabled.
    /// 
    void OnDisable()
    {
        for (int i = 0; i < texts.Count; i++)
        {
            Destroy(texts[i].Rect.gameObject);
            texts[i] = null;
            texts.Remove(texts[i]);
        }
    }

   //Privates
    private static List texts = new List();

静态的= =

想不明白,为什么会报错呢,难道ondisable是在场景销毁了之后才执行的?

根据ondisable里的代码,大致知道他是想要把生成的text删除,然后再从列表里删除

但是为什么不直接new一个呢?可能是考虑到其他别的情况吧,比如我目前这个情况就不需要删除了,因为跳转场景的时候就自动删除了,只需要删除列表里的就行,于是我将ondisable中的代码改成这样。。。

    /// 
    /// Disable all text when this script gets disabled.
    /// 
    private void OnDisable()
    {
        texts = new List();
    }

目前看来是没有报错的啦= =

不知道其他地方有没有受到影响呢


你可能感兴趣的:(Unity3D_原创)