How to add a child(note) via Code

Question

How to add a childnote via Code? I queue_free() the camera and now i want to add a new one to my player

var new_camera = Camera2D.new()

func _reset_player():
get_tree().root.add_child(new_camera)

#i also tried 
get_tree(self).root.add_child(Camera2D)

This is whose script you tried to reset? And what happened?

oh no, i just wanna reset some variables and a camera note.
i get the following Error " Attempt to call function “add_child” in base null instance on a null instance.

and i have no idea what a null instance is

It means that get_tree().root is null, I am not sure what you want to do, but if you want to add camera to current scene then you can use get_tree().current_scene, if in player, then define a player variable in main script and use get_tree().current_scene.player

1 Like