赞
踩
1.执行第4章中4.6节创建切换UI的调度器的图List 4-3代码时,易报错:“NullReferenceException: Object reference not set to an instance of an object”(P147)
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.UI; //使用UI组件时要先引入它
-
- public class GameDirector : MonoBehaviour{
- GameObject car;
- GameObject flag;
- GameObject distance;
-
- void Start(){
- this.car = GameObject.Find("car");
- this.flag = GameObject.Find("flag");
- this.distance = GameObject.Find("Distance");
- }
-
-
- void Update(){
- float length = this.flag.transform.position.x - this.car.transform.position.x;
- this.distance.GetComponent<Text>().text = "距离目标" + length.ToString("F2") + "m";
- }
- }
List 4-3 "显示距离信息"的脚本
如果在P142创建UI时
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。