How to add instances using function from another script?

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

Main.tscn
var spawner = load("*path to spawner.gd*").new()

func _physics_process(delta):
spawner.spawn()

Spawner.gd
var load_type = preload(“thing.tscn”)

var instance = load_type.instance()
fun spawn():
add_child(instance)
move_child(instance, 3)

But the problem is nothing changes.

I typed the line below add_child print(instance.get_path())

Output is:

Cannot get path of node as it is not in a scene tree.

:bust_in_silhouette: Reply From: klaas

Hi,
When spawner is not in the scene tree the added child will mit appear in the scene.