当前位置:   article > 正文

学习《Unity2018入门与实战》中易出现的错误_unity text legacy

unity text legacy

1.执行第4章中4.6节创建切换UI的调度器的图List 4-3代码时,易报错:“NullReferenceException: Object reference not set to an instance of an object”(P147)

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI; //使用UI组件时要先引入它
  5. public class GameDirector : MonoBehaviour{
  6. GameObject car;
  7. GameObject flag;
  8. GameObject distance;
  9. void Start(){
  10. this.car = GameObject.Find("car");
  11. this.flag = GameObject.Find("flag");
  12. this.distance = GameObject.Find("Distance");
  13. }
  14. void Update(){
  15. float length = this.flag.transform.position.x - this.car.transform.position.x;
  16. this.distance.GetComponent<Text>().text = "距离目标" + length.ToString("F2") + "m";
  17. }
  18. }

List 4-3 "显示距离信息"的脚本  

如果在P142创建UI时

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/很楠不爱3/article/detail/140746
推荐阅读
相关标签
  

闽ICP备14008679号