《unity手机游戏开发》4.7敌人Object reference not set to an instance of an object
2017-07-29 21:07
465 查看
通过这本书学习unity发现Object reference not set to an instance of an object这个问题
通过谷歌大法了解到是未实例化的问题,仔细检查了代码,发现书籍给的代码中myCurrentPathNode未赋值
所以在enemy.cs中加了如下代码
这是修正后enemy.cs的完整代码
通过谷歌大法了解到是未实例化的问题,仔细检查了代码,发现书籍给的代码中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)); } }
相关文章推荐
- TFS 客户端错误"Object reference not set to an instance of an object."
- Submit Reporting Table Error : Object reference not set to an instance of an object.
- NullReferenceException: Object reference not set to an instance of an object
- 解决调用context.Session["NAME"]时总出现Object reference not set to an instance of an object.异常的方法
- WebAdaptor Object reference not set to an instance of an object.
- NET:Error Creating Control -"Object Reference Not Set To An Instance Of Object"
- Object reference not set to an instance of an object
- SQLite应用程序启动报错:Object reference not set to an instance of an object.
- 关于Unity3D的错误:NullReferenceException: Object reference not set to an instance of an object 的解答
- 新建silverlight项目出现object reference not set to an instance of an object的错误
- Visual Studio 2015打开ASP.NET MVC的View提示"Object reference not set to an instance of an object"错误的解决方案
- silverlight项目出现object reference not set to an instance of an object的解决办法
- “Object reference not set to an instance of an object. ”错误
- 20100507 学习记录: Server Error in '/' Application. Object reference not set to an instance of an object
- 当在一个siliverlight 2.0工程中 添加webservice时 遇到错误:'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.
- 添加wcf服务时弹出"an object reference not set to an instance of an object "
- ASP.NET MVC Html.TextBox 报 Object reference not set to an instance of an object 的解决方案