Why is AnimationPlayer null-instance in -ready()?

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

Here is my code:

onready var anim_player : AnimationPlayer = find_node(“Anim”)

func _ready():
assert(anim_player) != null
match(activeanimtype):
“_full”: anim_player.play(“fullcycle”)
“_half”: anim_player.play(“halfcycle”)

The assert is failing as anim_player is null. The name of the AnimationPlayer is “Anim”.

:bust_in_silhouette: Reply From: jgodfrey

Where is the AnimationPlayer in the scene tree compared to the node that has the above script attached? find_node() only works on descendant nodes (so, nodes that live somewhere beneath the node containing the script).

Also, pay special attention to the other args that find_node accepts, as they’re important in certain situations.

I solved the priblem using get_parent().find_node().

Thanks!

bribat123 | 2020-02-17 16:55