I'm trying to animate a character but it's throwing an error(I literally just started the project, I'm so frustrated)

extends CharacterBody2D

@onready var animated_sprite_2d = AnimatedSprite2D

@export var move_speed: float = 70.0

var direction: Vector2 = Vector2.ZERO

func _process(delta):
direction = Vector2.ZERO

if Input.is_action_pressed("move_left"):
	animated_sprite_2d.play("side")
	animated_sprite_2d.flip_h = true

Question

`Hello everyone I’m new to Godot and this is really frustrating. I’m getting an error

“3:Cannot call non-static function “play()” on the class “AnimatedSprite2D” directly. Make an instance instead.”

Hello!
You did it right, but NOT correctly use your path to animated sprite node.
You should :

  1. right click on animated sprite and make it “Access by unique name”.
  2. @onready var animated_sprite_2d = %AnimatedSprite2D

Now this is PATH to your node and assigned variable to it.

Oh my goodness thank you so much!!!

func _process(delta):
direction = Vector2.ZERO

if Input.is_action_pressed("move_left"):
	animated_sprite_2d.play("side")
	animated_sprite_2d.flip_h = true
	direction = Vector2.LEFT

elif Input.is_action_pressed("move_right"):
	animated_sprite_2d.play("side")
	animated_sprite_2d.flip_h = false
	direction = Vector2.RIGHT

elif Input.is_action_pressed("backward"):
	animated_sprite_2d.play("back")
	animated_sprite_2d.flip_h = false
	direction = Vector2.UP

elif Input.is_action_pressed("forward"):
	animated_sprite_2d.play("front")
	animated_sprite_2d.flip_h = false
	direction = Vector2.DOWN

This is code to run the character animation but it has thrown up a new error when I try to run it throws "attempt to call function ‘play’ in base null instance on a null instance "

Please could you help me with this too. Thank you so much

I recommend you put a print of your scene structure to make easier help u.

I’m sorry I’m new to this. I’m trying to follow a tutorial https://www.youtube.com/watch?v=nYN4L8afRfQ&t=51s its at 15:03

extends CharacterBody2D

@onready var animated_sprite_2d = %AnimatedSprite2D

@export var move_speed: float = 70.0

var direction: Vector2 = Vector2.ZERO

func _process(delta):
direction = Vector2.ZERO

if Input.is_action_pressed("move_left"):
	animated_sprite_2d.play("side")
	animated_sprite_2d.flip_h = true
	direction = Vector2.LEFT

elif Input.is_action_pressed("move_right"):
	animated_sprite_2d.play("side")
	animated_sprite_2d.flip_h = false
	direction = Vector2.RIGHT

elif Input.is_action_pressed("backward"):
	animated_sprite_2d.play("back")
	animated_sprite_2d.flip_h = false
	direction = Vector2.UP

elif Input.is_action_pressed("forward"):
	animated_sprite_2d.play("front")
	animated_sprite_2d.flip_h = false
	direction = Vector2.DOWN
	
velocity = direction * move_speed
move_and_slide()

No problem, but what i talked about is this:


We need to know your scene is structured because you’re getting a null instance, to help you get the correct value we need to know how your nodes are organized.

this is it thank you

In this case you just need to do equal the video u send: @onready var animated_sprite_2d = $AnimatedSprite2D. You forget the “$” before the AnimatedSprite2D

You should find info about NODE PATH on Youtube.
If you use @onready var animated_sprite_2d = %AnimatedSprite2D, then your NODE in scene tree, must be “Access by unique name”, “%” - sign that trigger Godot to find node by unique name.
If NODE does not use unique name, then you should use “$” before your node name. Thats the point - you need PATH to node.