How do i instance a scene multiple times?

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

i have some code to spawn a cube on the player when i press R, but whenever i do it just teleports the cube to where the player is instead of spawning a new one. i probably did this wrong, so some help would be apreciated. heres my code:

var cube = load("res://cube.tscn").instance()

func _process(delta):
if Input.is_action_just_pressed("R"):
	get_tree().root.add_child(cube)
	cube.global_transform = self.global_transform

im pretty new to this so sorry if this is a stupid question

:bust_in_silhouette: Reply From: Beureuuu
 var cube = load("res://cube.tscn") 
 var cube_node
 
 func _process(delta): if Input.is_action_just_pressed("R"):
     cube_node = cube.instance()
     get_tree().root.add_child(cube_node)
     cube_node.global_transform = self.global_transform

Try something like this maybe ? I’m myself new too, but I believe that instancing your cube directly is what makes it keep the same “reference”, kind of.
Instancing a new one on each key press instead should work

that worked! thank you so much.

ironcat | 2021-06-27 00:00

You need to instance scene multiple times to get it back multiple times. The mistake you were doing was that you instanced your cube only once. This caused the cube to teleport cause there was only one cube instance.

i.e to instance multiple cubes instantiate it in the _process function.