码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2015-07-11 16:43:59      阅读:185      评论:0      收藏:0      [点我收藏+]

标签:

//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程序,,
  }
}

 

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

标签:

原文地址:http://www.cnblogs.com/LuRain/p/4638833.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!