How do I store new instances in arrays?

Godot Version

4.3 stable

Question

I’m trying to duplicate and move down a ColorRect every time you press a button. I’m using instances, and currently, that’s working fine- but I want to store its data in an array so I can delete a specific instance.
Code:

@onready var box = preload("res://Scenes/ColorRect.tscn") #preloading the scene with a box I want to dupe

func _update_text(): #called function when duplicating
	var s = box.instantiate() #creates a new instance
	#somehow store the instance number in array
	if delete_button_pressed == true: #this isn't exact, I paraphrased it for your convenience 
	#somehow call instance using array and free it from queue

you can just create an array and append it:

var boxes: Array = []

func _update_text(): #called function when duplicating
	var s = box.instantiate() #creates a new instance
	boxes.append(s)
1 Like