Hi, I am getting a Null Reference exception when I run this simple C# code. I am sure it is a stupid oversight on my part.
I am able to assign a value to my label.Text in Ready() but not in Process(). Can somebody help me understand where I am going wrong? I need a fresh set of eyes.
using Godot;
using System;
public class Player : KinematicBody2D
{
public int speed = 100; //Movement speed of character
public Vector2[] path; //Path to be followed
public int currentPoint = 1; //The current point in the path
public AnimatedSprite sprite;
public Label label;
public override void _Ready()
{
AnimatedSprite sprite = GetNode<AnimatedSprite>("AnimatedSprite");
Label label = GetNode<Label>("Label");
label.Text = Name;
SetProcess(true);
}
public override void _Process(float delta)
{
var moveDistance = speed * delta;
MoveAlongPath(moveDistance);
}
public void MoveAlongPath (float distance)
{
var startPoint = this.Position;
if(path != null)
{
if (path.Length > 0)
{
var distanceToNext = this.Position.DistanceTo(path[currentPoint]);
if(distance <= distanceToNext && distance >= 0.0)
{
this.Position = startPoint.LinearInterpolate(path[currentPoint], distance/distanceToNext);
this.label.Text = "Moving"; //*** CRAPS OUT HERE *** <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
if(Position.x - path[currentPoint].x > 0) {GD.Print("left");}
if(Position.x - path[currentPoint].x < 0) {GD.Print("right");}
}
else
{
GD.Print("idle");
if(currentPoint < path.Length -1 ) { currentPoint++;}
}
}
}
}
public void _on_MainScene_MoveUnit(Vector2[] UnitPath, int NavPoint)
{
path = UnitPath;
currentPoint = NavPoint;
}
}