So, I'm new on Godot and I try to make button can play the animation to move some UI, here my code.

extends Button
	
func _on_ExtraButton_pressed():
	get_node("AnimationPlayer").play("HideExit")

But I always get error messenger like this

E 0:00:01.693   get_node: Node not found: AnimationPlayer.
  <C++ Error>   Condition "!node" is true. Returned: __null
  <C++ Source>  scene/main/node.cpp:1381 @ get_node()
  <Stack Trace> ExtraButton.gd:4 @ _on_ExtraButton_pressed()

It weird, because AnimationPlayer already on scene

get_node uses a relative path. And since AnimationPlayer is not a child of the button, that code won't work.

You can use this instead:

get_node("/root/Control/AnimationPlayer").play("HideExit")

@cybereality said: get_node uses a relative path. And since AnimationPlayer is not a child of the button, that code won't work.

You can use this instead:

get_node("/root/Control/AnimationPlayer").play("HideExit")

Thank you, now I don't get any errors, but now the animation doesn't play when I click the button.

Maybe try this, so the node path is relative:

get_parent().get_parent().get_node("AnimationPlayer").play("HideExit")

Then if the scene is instanced or something like that, it should work.

You can also use an exported NodePath if you want, so then you can set the AnimationPlayer through the Godot editor:

export (NodePath) var path_to_anim

_on_ExtraButton_pressed():
	get_node(path_to_anim).play("HideExit")

Then to set up the NodePath you just need to select the node in the Godot editor and go to the inpsector, then there should be a property for the NodePath. :smile:

or simply move up the stack with get_node("../../AnimationPlayer").play("HideExit")

@Megalomaniak said: or simply move up the stack with get_node("../../AnimationPlayer").play("HideExit")

Yeah, that is a much cleaner way to do it, especially compared to the get_parent version :lol:

And another variation: $"../../AnimationPlayer".play("HideExit")

I'm not sure which is more readable.

2 years later