How to spawn multiple objects in different locations?

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

Hello, I am attempting to add multiple of the same child depending on the player’s location away from the previously spawned child

var hittingObstacle = false
var hitFossil = false
var playerDigLoc
var HoleSpawnLocation = preload("res://Characters/Hole/hole_spawner.tscn")
var hole_node = HoleSpawnLocation

func _ready():
	playerDigLoc = get_parent().get_node("Player/HoleLocation")

@warning_ignore("unused_parameter")
func _process(delta):
	if Input.is_action_pressed("ui_accept") and !hittingObstacle and !hitFossil:
		add_child(hole_node.instantiate())
		global_position = playerDigLoc.global_position # I believe that this is what is causing part of the problem, for every time I press the action button, it just takes the previously spawned child to the current location of the spawning area.
		await get_tree().create_timer(4.0).timeout

if anybody can help me find a solution to this problem, I’ll be grateful. Thank you all in advance.

:bust_in_silhouette: Reply From: stormreaver

You need to set the global_position of the hole_node to your player’s dig location, but you’re setting the global position of whatever object your script represents to the global position of your player’s dig location.

So instead of:

global_position = playerDigLoc.global_position

try

hole_node.global_position = playerDigLoc.global_position