Ich versuche, Spiel in Einheit zu machen und Skripte sind in C#. Ich habe diesen Fehler, wenn ich mein Objekt Bewegung in Richtung der KnotenNullReferenceException 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 machen: Objektverweis nicht auf eine Instanz eines Objekts Enemy.Update gesetzt() (bei Aktiva/Enemy.cs: 35) Hier ist der Fehler. diese
Nun 'if (targetPathNode = null)' sollte 'if (targetPathNode == null)' –
@ ŁukaszMotyczka Yup. Stimmt. Dies sollte als Tippfehler – Programmer
@am geschlossen werden und bitte zeigen Sie, welche Zeile 35 ist, da es keine Nummerierung gibt. Es könnte in der Zukunft helfen. –