Screen transition: attempt to call "play" in base null instance of a null instance

Godot Version

4.3

Question

extends CanvasLayer

@onready var anim_player = $AnimationPlayer

func change_scene(target: String) -> void:
	anim_player.play("dissolve")
	await anim_player.animation_finished
	get_tree().change_scene(target)
	anim_player.play_backwards("dissolve")

I moved stuff around, but then I made sure to remake the animation player from scratch yet I’m still getting this error. attempt to call function “play” in base null instance of a null instance.

The script failed to get anim_player, it is null. Maybe it is not a direct child of this script?

your anim player cannot be found at runtime (i guess) so cover it on the @onready… something like this…

extends CanvasLayer

@onready var anim_player = $AnimationPlayer

func _ready():
    # Debug check to verify if the AnimationPlayer exists
    if not anim_player:
        print("AnimationPlayer not found!")
        print("Available children:", get_children())

func change_scene(target: String) -> void:
    if not anim_player:
        push_error("AnimationPlayer is null!")
        return
        
    anim_player.play("dissolve")
    await anim_player.animation_finished
    get_tree().change_scene_to_file(target)  # Note: In Godot 4.x, use change_scene_to_file
    anim_player.play_backwards("dissolve")

Still saying the animation player is null.

animation player is a child of the canvas layer

is this a global? Did you add the script or the scene as a global? If the former then it won’t create any children, only the CanvasLayer.

It is global.

Then make sure you added the scene .tscn, not the script .gd

2 Likes

ehrm… null is nothing… print out your full tree to see where it resides… maybe you are missing some instantiated scene or so…

func _ready():
	# Start from the root of the scene tree
	var root = get_tree().root
	print_node_tree(root)

func print_node_tree(node: Node, indent: int = 0):
	# Create an indentation string based on the current level
	var prefix = "  ".repeat(indent)
	
	# Print the current node's name and type
	print(prefix + node.name + " (" + node.get_class() + ")")
	
	# Recursively print each child node with increased indentation
	for child in node.get_children():
		print_node_tree(child, indent + 1)

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.