Adding a child from an instance to the parent nodes in another scene

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

Hi
I’m trying to add this child to this node but nothing happens that way, what’s wrong?

extends Area2D

var SOUND = preload(“res://tscn/sound_effect.tscn”)

func _ready():
var v = SOUND.instance().get_child(2)
add_child(v)

sorry but why dont you instance the child directly?

Andrea | 2021-08-24 16:50

Do you mean something like this?

SOUND.instance().get_child(4).play()

Atef | 2021-08-24 17:47

no… i mean, if you need the child n.4 of the scene SOUND, why did you created the scene SOUND only to get the child?
couldnt you create a scene CHILD_n_4 and instance that?

by the way, i think it does not work cause parent.add_child(child) only works if child does not has a parent.
so you first need to remove the parent of the child, and then add the child to another parent

Andrea | 2021-08-24 17:58

:bust_in_silhouette: Reply From: Andrea
var SOUND = preload("res://tscn/sound_effect.tscn")

func ready():
var s=SOUND.instance()
var v = s.get_child(n)
s.remove_child(v)
add_child(v)
s.queue_free() #

Yes, thank you

Atef | 2021-08-24 18:20