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的相关代码是这样的

    /// <summary>
    /// Disable all text when this script gets disabled.
    /// </summary>
    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<bl_Text> texts = new List<bl_Text>();

静态的= =

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

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

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

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

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

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


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值