Getting Null Instance on AnimationPlayer, although it shows up as node in the Remote Scene Tree

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

Hi all,
I’ve been looking for an answer to this question, but I haven’t found anythiing yet that got me further.

I’m trying to build an Item base class with the following scene tree:

  • Item (Node2d)
  • Item Sprite (Sprite2D)
  • ItemBody (RigidBody2D)
    • ItemCollisionShape (CollisionShape2D)
  • FloatingAnimation (AnimationPlayer)

The corresponding looks like this:

extends Node2D
class_name Item

var gravity = ProjectSettings.get_setting("physics/2d/default_gravity")

# Called when the node enters the scene tree for the first time.
func _ready() -> void:
	var item_body : RigidBody2D = $ItemBody
	var item_animation : AnimationPlayer = $FloatingAnimation
	print_debug("Animation: %", % item_animation.get_animation_list())

Trying to run my project then i get:

Attempt to call function 'get_animation_list' in base 'null instance on a null instance
E 0:00:00:0581 @ _ready(): Node not found: "%item_animation" (relative to "/root/Level/Item").
  <C++ Error>    Method/function failed. Returning: nullptr
  <C++ Source>   scene/main/node.cpp:1364 @ get_node()
  <Stack Trace> @ _ready()

Now, in all the cases where I found this question come up, the first and natural question was, what the scene tree at runtime looks like, which is, where i get really confused. According to the debugger, the node item_animation shows up as a non-null object in the debugger:

Stack Variables

As well as in the Remote scene tree:

Remote Scene Tree

Since I apparently cant add the screenshots directly, here are the direct links

Stack Variables in Debugger
Remote Scene Tree

I’m a little bit at a loss as to what might be happening here. I’d be really grateful for any hint on how to resolve this.

:bust_in_silhouette: Reply From: Zylann

You have a , after "Animation: %". Removing it should fix it.

Which confusingly means that Godot interprets % item_animation as %item_animation (without space), which is a new feature called “unique scene nodes”, in which you use % to refer to nodes with a unique name in the scene without using their path.

Of course I guess your item scene does not use this feature, but that means Godot goes ahead and fails to find %item_animation so it results in a null in your print statement.

Thanks, that worked perfectly. I really just seem to have messed up the format string.

haru_kai | 2023-07-04 12:47