Instantiate, Timer

extends Node2D

var icon = preload(“res://Scenes/icon.tscn”)
var icon_inst = icon.instantiate()

func _ready():
pass

func _on_timer_spawner_timeout():
$Path2D/PathEnemy.add_child(icon_inst)

I want to instancing every second an instance, but only 1 appear and got error that says Cant add child to Path because, aleready Parent

You’re trying to add an already added instance to the parent.

Move var icon_inst = icon.instantiate() to the timeout function so you can instantiate icon_inst on every timeout.

1 Like

Okay but in game there is only one sprite in screen, when we go to remote > root, now we see 1 sprite with correct name that I gave and every second is with generated named by godot like Sprite2D@2, Sprite2D@3…

Unless you specifically set the names of objects instantiated via code, Godot will automatically assign names to them.

The sprites are probably just being instantiated in the same position. Try changing the position of each instantiated sprite.

why do you need it to be the name you want? you call the node by nodepath?, better not, because it’s not realiable. even though you can always rename it with .name
what you can do to get the node you wanted from these instantiated node is either store them into an Array of Nodes. or use the parent and actually use .get_children() function to get all the instantiated nodes

My problem it’s not the name of instance, I’m just to explaining there is a lot of instance created.
The problem is all the instance seems to refer one progress ratio, that so just one sprite2D will see in the screen play.

extends PathFollow2D

var speed: float = 0.06

var icon = preload(“res://Scenes/icon.tscn”)

var icon_inst

func _ready():
icon_inst = icon.instantiate()

func _process(delta):
progress_ratio += speed * delta

func _on_timer_spawner_timeout():
add_child(icon_inst)

var icon = preload(“res://Scenes/icon.tscn”)

var icon_inst

func _ready():
pass

func _process(delta):
progress_ratio += speed * delta

func _on_timer_spawner_timeout():
icon_inst = icon.instantiate()
add_child(icon_inst)

This code give me no error but the sprite not show up in the game.

var icon = preload(“res://Scenes/icon.tscn”)

var icon_inst

func _ready():
pass

func _process(delta):
progress_ratio += speed * delta

func _on_timer_spawner_timeout():
icon_inst = icon.instantiate()

This would be because you forgot to add the line that adds it as a child. Your code:

func _on_timer_spawner_timeout():
icon_inst = icon.instantiate()

The code that is needed:

func _on_timer_spawner_timeout():
icon_inst = icon.instantiate()

#add this line here:
add_child(icon_inst)

Thanks for your attention, but this code already performed.

func _on_timer_spawner_timeout():
icon_inst = icon.instantiate()

#add this line here:
add_child(icon_inst)

You can see on left picture there is instances generated but on the right (in game window) just one sprite appear.

What does Icon.tscn look like? Is it just a Sprite2D with the icon? Do they have/need a PathFollow node? Are the icon instances supposed to be in different places? Is there anything putting them in those places if they are? Is there really just one, or are they hidden or stacked?

That icon just moving follow the path, I want they spawn every second.

I guess the instances follow same progress as the first icon.