Attempt to call function 'add_child' in base 'null instance' on a null instance.

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

help! godot kep calling this error, this is the code:

onready var Alien = get_tree().get_root().get_node("Level 1/Alien")
onready var muzzle = get_tree().get_root().get_node("Level 1/Player/Muzzle")

func _ready():
	if Global.araragi:
		#var copy = preload ("res://Actors/Alien.tscn") 
		var copy = Alien.duplicate()
		owner.add_child(copy)
		copy.transform = $Muzzle.global_transform

the error is in the line “owner.add_child(copy)” I used duplicate and preload but none works, I even use the print to know that “copy” exist and tells me its name is KinematicBody2D:1261, but still doesnt work.

I used a similar code in other scene and worked fine.

Try to run print(owner), it looks like owner is null

sarapan_malam | 2022-09-05 10:25

you are right! owner is the one null, but I don’t understand why it worked in another scene but not here

enrap | 2022-09-05 22:34

:bust_in_silhouette: Reply From: whiteshampoo

at first, please download the current version of Godot (3.5) an use that instead. 3.2 is pretty old.

The problem is not copy, but owner.
I can only guess, but probably you want get_tree().current_scene instead.

you are rgiht, owner is null haha, thank you, what I don’t understand is why it worked in another scene though

so I use the get_tree().current_scene instead of owner? and sorry for asking too much, I’m a noob in game programming

I’ll download the latest godot version too

enrap | 2022-09-05 22:36