《unity手机游戏开发》4.7敌人Object reference not set to an instance of an object
来源:互联网 发布:小阴唇肥大 知乎 编辑:程序博客网 时间:2024/05/30 23:03
通过这本书学习unity发现Object reference not set to an instance of an object这个问题
通过谷歌大法了解到是未实例化的问题,仔细检查了代码,发现书籍给的代码中myCurrentPathNode未赋值
所以在enemy.cs中加了如下代码
private void Awake() { myCurrentPathNode = GameObject.FindGameObjectWithTag("pathnode").GetComponent<PathNode>(); }
这是修正后enemy.cs的完整代码
using System.Collections;using System.Collections.Generic;using UnityEngine;public class enemy : MonoBehaviour { public PathNode myCurrentPathNode; public int myLife = 15;//生命 public int maxMyLife = 15; public float myspeed = 2; //敌人的类型 public enum TYPE_ID { GRCUND, AIR, } public TYPE_ID myType = TYPE_ID.GRCUND; private void Awake()//这个是关键 { myCurrentPathNode = GameObject.FindGameObjectWithTag("pathnode").GetComponent<PathNode>(); } private void Update() { RotateTo(); MoveTo(); } //转向下一个路点 public void RotateTo() { float current = transform.eulerAngles.y; transform.LookAt(myCurrentPathNode.transform); Vector3 target = transform.eulerAngles; float next = Mathf.MoveTowardsAngle(current, target.y, 120 * Time.deltaTime); transform.eulerAngles =new Vector3(0, next, 0); } //向下一个路口移动 public void MoveTo() { Vector3 thisPosition = transform.position; ; Vector3 currentPathNodePosition = myCurrentPathNode.transform.position; //距离子路点的距离 float distance = Vector3.Distance(new Vector2(thisPosition.x, thisPosition.z), new Vector2(currentPathNodePosition.x, currentPathNodePosition.z)); if(distance<1.0) { if(myCurrentPathNode.childPathNode==null) { GameManager.instance.setDamage(1); Destroy(this.gameObject); } else { myCurrentPathNode = myCurrentPathNode.childPathNode; } } transform.Translate(new Vector3(0, 0, myspeed * Time.deltaTime)); }}
阅读全文
1 0
- 《unity手机游戏开发》4.7敌人Object reference not set to an instance of an object
- Object reference not set to an instance of an object.
- Object reference not set to an instance of an object.
- Object reference not set to an instance of an object
- Unity: “NullReferenceException: Object reference not set to an instance of an object”的可能解決方法
- object reference not set to an instance of an object" - Not "initialized" through WCF?
- WebBrowser "object reference not set to an instance of an object
- VB.NET Error Object reference not set to an instance of an object
- 我遇到的Object reference not set to an instance of an object.错误的解决办法
- Submit Reporting Table Error : Object reference not set to an instance of an object.
- 学习记录: Server Error in '/' Application. Object reference not set to an instance of an object
- 新建silverlight项目出现object reference not set to an instance of an object问题
- designer errors: Object reference not set to an instance of an object
- C#操作XML报Object reference not set to an instance of an object
- silverlight项目出现object reference not set to an instance of an object的解决办法
- NullReferenceException: Object reference not set to an instance of an object
- NullReferenceException: Object reference not set to an instance of an object
- 打开页面出现Object reference not set to an instance of an object错误
- 学以致用——利用直方图分析个股涨幅(Excel+Spotfire)
- 【C语言】文件
- Java基础拾遗(二)
- Redis学习手册--持久化机制
- css3 2d/3d变换——实现超炫的特效
- 《unity手机游戏开发》4.7敌人Object reference not set to an instance of an object
- 7.29笔试
- C语言基础之数组
- 博客搬家
- 【简记】Java Web 内幕——SpringMVC总体介绍,部分源码解析
- 【C#机房重构】三层TO七层
- 行人再识别(行人重识别)【包含与行人检测的对比】
- 一些字符串操作函数的模拟实现
- BZOJ 1059: [ZJOI2007]矩阵游戏 二分图匹配