add child to Control node

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

Hi
I try the code down bellow

extends Control

@onready var n = load("res://n.tscn")

 func _ready():
     add_child(n)

but i get error
Invalid type in function ‘add_child’ in base ‘Control ()’. The Object-derived class of argument 1 (PackedScene) is not a subclass of the expected argument class.

:bust_in_silhouette: Reply From: jgodfrey

You need to create an instance of the scene and then pass it to add_child(). So, this should work:

extends Control

@onready var n = load("res://n.tscn")

 func _ready():
     var instance = n.instantiate()
     add_child(instance)