Attention | Topic was automatically imported from the old Question2Answer platform. | |
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 :- )