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。

posted @ 2015-07-21 10:37  灵魂重新  阅读(259)  评论(0编辑  收藏  举报