get_node cannot find a node

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

Hi, I am trying to animate a sprite, so i set few animation, but when I call the function

if Input.is_action_pressed("move_up"):
	vel.y -= 1
	facingDir = Vector2(0, -1)
	$AnimationPlayer.play("move_up")

this error shows up

E 0:00:02.596 get_node: (Node not found: “AnimationPlayer” (relative to “/root/Player”).)
<Errore C++> Condition “!node” is true. Returned: nullptr
<Sorgente C++>scene/main/node.cpp:1325 @ get_node()
Player.gd:20 @ _physics_process()

now, I have the AnimationPlayer directly under the Player node, and also from script it autocomplete. I do not really know what I am doing wrong

If the script is attached to the Player node and the AnimationPlayer is a child of the Player, the code should run, unless you somehow delete the node on runtime. Does get_children() return a list including the AnimationPlayer?

skysphr | 2022-03-27 14:13

Maybe you are reparenting it at runtime somewhere in your code.

magicalogic | 2022-03-27 19:58

The same has happened to me a few times.
Change node name

ramazan | 2022-03-28 08:16

:bust_in_silhouette: Reply From: U23RN6uvgo

try using the get_node_or_null function instead of the dollar sign

somewhat like this:

if get_node_or_null("AnimationPlayer") != null:
    print(get_node("AnimationPlayer"))

If the code above doesn’t print anything, take a screenshot of your scenetree and animation player node’s name. I’ll try to help and see what the issue is. Expect a response in less than 24 hours