I have a simple scene, which inherits from Node2D and contains an Animation Player and a Sprite2D:

Then in the following script the variable animation_player, which should work fine, is NULL:

extends Node2D
class_name PickUpEffect


@onready var animation_player = $AnimationPlayer


func start_pick_up_effect() -> void:
	animation_player.play("idle")


func _on_animation_ends() -> void:
	queue_free()

What am I doing wrong?

Is this the line that causes the error?
animation_player.play("idle")

That error would occur if the function start_pick_up_effect() is called before the node PickUpEffect is "ready".

    4 days later

    DaveTheCoder something weird happens because if I do print(animation_player) in the _ready() function it also shows that the value of the var is null.

      Who calls start_pick_up_effect() ?
      Post the complete code of all involved scripts.

      svprdga the _ready() function

      Place another print statement in the _ready() function for the AnimationPlayer node, and see if it's executed before or after that print statement. Use print_debug() instead of print() to make it easier to tell where the printing occurs.

      I finally was able to solve it by re-creating the script with the same content as before, thanks for the help.