Unity3D技术:用一个bug说明Destroy、=null的区别,Vector3类型的特性。

//W,0,
using System;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using System.Collections;
using System.Threading;
public class DestroyBug : MonoBehaviour
{//PBLR
  //W1,1,-0
  //+,GO,对象,OB END,
  /*
运行这段代码会出现:NullReferenceException: Object reference not set to an instance of an object
这是Vector3类型发生Null反射造成的。
原理如下:
1.Vector3类型不支持null;
2.GameObject=null,position作为transform的子类,被置成null
3.试图读取时,就会出错。
*/
  //定义一个GameObject
  GameObject GOP;
  //W2,2,
  //+,PGO,公共对象,,指定GameObject的父节点
  public GameObject GOPR;
  //W3,3,
  //+,VE,三维数,,指定一个三维数
  Vector3 VEP = new Vector3(0.0f,0.0f,0.0f);
  //W4,4,
  //+,OC,主程序,,
  void Start(){//PBLR
    //W5,5,
    //+,CRGO,创建标准对象,,创建一个GameObject
    GOP =  GameObject.CreatePrimitive(PrimitiveType.Cube);
    GOP.transform.parent=GOPR.transform;
    //W6,6,
    ////+,DSGO,清空对象,,//
    //Destroy(GOP);
    //W7,7,
    //+,=,左等式,,GameObject=null(上一行是正确写法)
    GOP=null        ;
    //W8,8,
    //+,VE=,三维数,,试图读取position,报错。
    VEP = GOP.transform.position;
    //W9,9,
    //+,ES,End程序,,
  }
}

 

posted on 2015-07-11 16:02  LuRain  阅读(629)  评论(0)    收藏  举报

导航