zoukankan      html  css  js  c++  java
  • Destroy reall destroy ?!

    最近做战斗逻辑的时候发现一个问题

    测试脚本mTest:

    public class mTest : MonoBehaviour
    {
        public mTest2 tmp2;
    
    
        void OnGUI()
        {
            if (GUI.Button(new Rect(10, 150, 150, 100), "first click"))
            {
                Destroy(tmp2);
            }
               
    
            if (GUI.Button(new Rect(10, 10, 150, 100), "second click after first click some seconds"))
            {
                Debug.Log(" tmp2 == null : " + (tmp2 == null)); ///output : true
    
                Debug.Log(tmp2.value);
    
                tmp2.Test(); 
                ///output : test ///why !?
    
                Debug.Log(tmp2.name);
                ///output : MissingReferenceException: The object of type 'mTest2' 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.
            }
        }
    
    }
    

    测试脚本mTest2:

    public class mTest2 : MonoBehaviour
    {
       public string value = "11";
       public void Test() { Debug.Log("test"); }
    }
    

    奇怪的问题,如下解决:

     if (GUI.Button(new Rect(10, 150, 150, 100), "first click"))
     {
        Destroy(tmp2);
        tmp2 = null;
     }
    

    完全百思不得其解,已经是Null了,为什么还能调用其方法呢?u3d的bug吧,估计。

    所以以后我们要引用一个物体,销毁是我们调用的,那么最好设置一下引用为null。

  • 相关阅读:
    centos7 setfacl权限
    三层交换机做DHCP的一些疑问
    python3 re模块
    python3 的小爬虫
    初学python的一些简单程序(2)
    python3 字典
    python3 元组
    python3列表
    初学python的一些简单程序(1)
    python3的字符串操作
  • 原文地址:https://www.cnblogs.com/chongxin/p/4663691.html
Copyright © 2011-2022 走看看