My game is crashing and i dont know what to do!

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Sekaus

Hi, I’m using C# in my Godot game at I try to make a player move up, down, right, and left and it’s working fine but when I try to switch the animation of the animated sprite or Flip H the game start chasing and give this error message in the debugger:

E 0:00:09.979   void Player.GetInput(): System.NullReferenceException: Object reference 
not set to an instance of an object.
<C++ Error>   Unhandled exception
<C++ Source>  E:\MyGames\The Hoodie Gang\Scripts\Player.cs:51 @ void 
Player.GetInput()()
<Stack Trace> Player.cs:51 @ void Player.GetInput()()
Player.cs:27 @ void Player._PhysicsProcess(Single )()

and I not have tried to make it in GodotScript yet… but I don’t hope that the answer is that it only works in GodotScrpts and not in C# but here is the source:

using Godot;
using System;

public class Player : KinematicBody2D
{
int money = 0, speed = 200;
AnimatedSprite animatedSprite;
Vector2 velocity = new Vector2();

/*Texture[] sprites = new Texture[3] {
	(Texture)GD.Load("res://Sprites/Test_b.png"),
	(Texture)GD.Load("res://Sprites/Test_f.png"),
	(Texture)GD.Load("res://Sprites/Test_s.png")
};*/ 

void _Ready() {
	 animatedSprite = GetNode<AnimatedSprite>("Sprite");
}

/*void _Process() {
	
}*/

//Movement
public override void _PhysicsProcess(float delta)
{
	GetInput();
	velocity = MoveAndSlide(velocity);
}

public void GetInput()
{
	velocity = new Vector2();

	if (Input.IsActionPressed("move_right"))
		velocity.x += 1;

	if (Input.IsActionPressed("move_left"))
		velocity.x -= 1;
		
	if (Input.IsActionPressed("move_down"))
		velocity.y += 1;

	if (Input.IsActionPressed("move_up"))
		velocity.y -= 1;

	velocity = velocity.Normalized() * speed;
	
	//Animations
	if (velocity.x > 0) {
		animatedSprite.Animation = "Walk_h";
		animatedSprite.FlipH = true;
	}
	
	else if (velocity.x < 0) {
		animatedSprite.Animation = "Walk_h";
		animatedSprite.FlipH = false;
	}
	
	else if (velocity.y > 0) {
		animatedSprite.Animation = "Walk_op";
		animatedSprite.FlipH = false;
	}
	
	else if (velocity.y < 0) {
		animatedSprite.Animation = "Walk_Down";
		animatedSprite.FlipH = false;
	}
}
}

I don’t know if is a bug or something else… but I hope you can help me :- )

:bust_in_silhouette: Reply From: exuin

Line 51 is

    animatedSprite.Animation = "Walk_h";

A System.NullReferenceException means that animatedSprite does not refer to a node like it should. I think you should check if you’re getting the path to the node correctly in the _Ready() function.

Sorry, I’m new at Godot… I tried writing this in the _Ready() function

GD.Print( "Sprite: " + GetNode<AnimatedSprite>("Sprite")); 

but I can’t find or get any output… but I could try to check the path to the node to animatedSprite in another way…

Sekaus | 2021-05-19 14:40

You have a node called “Sprite” as a child of your KinematicBody2D, right?

exuin | 2021-05-19 15:21

No… it’s called AnimatedSprite… that must be the problem…

[Edit]
Okay I changed the _Ready function to be like this instead:

void _Ready() {
	animatedSprite = GetNode<AnimatedSprite>("AnimatedSprite");
	GD.Print( "AnimatedSprite: " + animatedSprite);
}

but I get the same error…

E 0:00:03.749   void Player.GetInput(): System.NullReferenceException: Object 
reference not set to an instance of an object.
<C++ Error>   Unhandled exception
<C++ Source>  E:\MyGames\The Hoodie Gang\Scripts\Player.cs:62 @ void 
Player.GetInput()()
<Stack Trace> Player.cs:62 @ void Player.GetInput()()
Player.cs:28 @ void Player._PhysicsProcess(Single )()

Sekaus | 2021-05-19 16:51