Animation names are null even with AnimationPlayer being child and var

trying to play an animation on interaction and i get [Cannot call method ‘play’ on null value]


@onready var interactable: Area2D = $Interactable
@onready var day_and_night: StaticBody2D = $day_and_night
@onready var anim: AnimationPlayer = $AnimationPlayer




func _ready() -> void:
	interactable.interact = _on_interact
	anim.play("night_to_day")



func _on_interact():
	if Input.is_action_pressed("interact"):
		anim.play("day_to_night")```

It’s not that your animation names are null, it’s your anim variable that is null.
Make sure that this reference you are assigning to this variable is correct.

@onready var anim: AnimationPlayer = $AnimationPlayer
1 Like

As @wchc said, the variable may be null due to a reference problem, meaning that the code is looking for something in the wrong place therefore it assigns null since it didn’t find anything.

I can also think of another possibility, which is a timing issue. when this node is ready the node you are trying to reference it is still not ready and so the variable is assigned to null.

If you are not able to solve this, share your node structure.