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。

  • 相关阅读:
    演义江湖PC端意见汇总
    演义江湖移动端内存优化意见汇总
    BitmapData类介绍
    我的职业规划_2013-7-29
    总结调用Flash的几种方法
    Pycharm注册
    删除表
    读取url中某个值
    Pycharm常用快捷键
    pip3使用
  • 原文地址:https://www.cnblogs.com/chongxin/p/4663691.html
Copyright © 2011-2022 走看看