Topic was automatically imported from the old Question2Answer platform.
Asked By
1izNoob
I want to do the following:
I have a control node and I want to create an HBoxContainer and multiple (identical) TextureRects inside of the HBoxContainer.
The code I have for that is this:
func _ready():
var ITEM = TextureRect.new()
ITEM.texture = load("res://item.png")
var row1 = HBoxContainer.new()
for i in 5:
row1.add_child(ITEM)
add_child(row1)
This almost works, the problem is that Godot only attaches one child, not 5. Can someone help?
You’re trying to attach the same instance multiple times. Do this instead:
func _ready():
var row1 = HBoxContainer.new()
for i in 5:
var ITEM = TextureRect.new()
ITEM.texture = load("res://icon.png")
row1.add_child(ITEM)
add_child(row1)
ah thank you!
Just so that I understand: Once the instance is created I can only attach this one as a child once?
1izNoob | 2020-03-21 13:01
Correct. An instance is a unique realization of a scene. Much like one person cannot be in two different places at the same time. Even though it might look like that when you meet identical twins.