+1 vote

My code looks like this:

onready var amount = 20
onready var coin = preload("res://coin.tscn").instance()

func _drop():

        for i in range(amount):
            amount -= 1


When this func is executed, it prints "coin" 20 times, but only add one child instance.

What do I have to do to add 20 children?

Thank you.

in Engine by (28 points)

1 Answer

+2 votes
Best answer

Try this: (untested)

onready var amount = 20
onready var coin = preload("res://coin.tscn")

func _drop():

        for i in range(amount):
            var new_coin = coin.instance()
            print("coin: ", new_coin.name)
            # the nodename might help to debug


I dont think you want to decrease amount in the loop. This will probaly give you bugs, if you use _drop() a second time.

I'm also not sure, if you need "onready" for these variables.

by (1,520 points)
selected by

It worked. Thank you!

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.