-3
我想在Unity中製作遊戲,並且腳本在C#中。我有這個錯誤,當我做我的對象邁向NullReferenceException Unity
using UnityEngine;
using System.Collections;
public class Enemy : MonoBehaviour {
GameObject pathGO;
Transform targetPathNode;
int pathNodeIndex = 0;
float speed = 5f;
public int health = 1;
// Use this for initialization
void Start() {
pathGO = GameObject.Find ("Path");
}
void GetNextPathNode(){
targetPathNode = pathGO.transform.GetChild (pathNodeIndex);
pathNodeIndex++;
}
// Update is called once per frame
void Update() {
if (targetPathNode = null) {
GetNextPathNode();
if (targetPathNode == null) {
// We've run out of path
ReachedGoal();
}
}
Vector3 dir = targetPathNode.position - this.transform.localPosition;
float distThisFrame = speed * Time.deltaTime;
if (dir.magnitude <= distThisFrame) {
// We reached the node
targetPathNode = null;
}
else {
// Move towards the node
transform.Translate(dir.normalized * distThisFrame);
//Quaternion targetRotation = Quaternion.LookRotation (dir);
this.transform.rotation = Quaternion.LookRotation (dir); //Quaternion.Lerp (this.transform.rotation, targetRotation, Time.deltaTime);
}
}
void ReachedGoal(){
Destroy (gameObject);
}
}
的NullReferenceException節點:對象引用未設置爲一個 對象Enemy.Update(實例)(在資產/ Enemy.cs:35 )這是錯誤。
井'如果(targetPathNode = NULL)'應該是'如果(targetPathNode == NULL)' –
@ŁukaszMotyczka燁。那就對了。這應該作爲錯字 – Programmer
@ amand關閉,因爲沒有編號,請指出哪一行是第35行。這可能有助於未來。 –