How to create a Node and add it as a child.

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

I tried this but doesn’t work.

	var sound = AudioStreamPlayer2D 
    add_child(sound)
:bust_in_silhouette: Reply From: kidscancode

You must create a new node of the given class:

var sound = AudioStreamPlayer2D.new()
add_child(sound)

Thank you …

usurun | 2019-05-22 15:43

What would be the name of the created node?
It just keeps returning null.

usurun | 2019-05-22 16:25

What is returning null? You really should include code and error messages (if any) with your questions. It saves a lot of time.

A newly created node is assigned an automatically generated name.

var sound = AudioStreamPlayer2D.new()
add_child(sound)
print(sound)

The result is @@2. If you want to assign a name use

sound.name = "NewName"

kidscancode | 2019-05-22 17:11

Thanks, I was writing it the wrong way.

usurun | 2019-05-22 18:15

I registered just to upvote your answer. Thanks a lot!

g2115 | 2021-07-30 16:42