How to spawn an object after one has been destroyed

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

I am starting out and used the zenva 50min game course as my base game, I am trying to make my game a platformer where a coin can spawn anywhere on the map and when you collect it another coin spawns randomly, I got it so that I can get 1 coin to randomly spawn after the first one is destroyed but after that it stops spawning more and I don’t know why.
here is my code
Main scene code:

var coin = preload("res://Coin.tscn")
func _on_Coin_body_entered(body): 
if body.is_in_group("Player"):
	randomize()
	var money = coin.instance()
	money.position = Vector2(rand_range(25, 950), rand_range(25,450))
	add_child(money)
	print("hello")

coin code:

func _on_Coin_body_entered(body): 
if body.is_in_group("Player"):
	Player.score += 1
	queue_free()
:bust_in_silhouette: Reply From: exuin

So I’m guessing that the function _on_Coin_body_entered() is triggered by a signal connected in the editor. However, new instanced coins do not have that signal. You must connect a signal through code, like this:

money.connect("body_entered", self, "_on_Coin_body_entered")

it now works but when I try to collect the instantiated coins they don’t disappear and just spawn more coins that I can’t collect

doorpen | 2021-09-17 01:39

Did you change any code or did you only add the code I posted?

exuin | 2021-09-17 02:24

I just added the code you posted, I am getting these errors when my player tries to collect the instantiated coins
error: area_set_shape_disabled: Can't change this state while flushing queries. Use call_deferred() or set_deferred() to change monitoring state instead.

error 2: emit_signal: Error calling method from signal 'body_entered': 'Area2D::_on_Coin_body_entered': Method not found.

doorpen | 2021-09-17 02:49

That’s weird, can I see your full code?

exuin | 2021-09-17 04:13

If you want code of every script I have I can give it to you but as they don’t really relate here is the code from the main scene and coin
MainScene:

extends Node2D

var coin = preload("res://Coin.tscn")

func _on_Coin_body_entered(body): 
    if body.is_in_group("Player"):
	    randomize()
	    var money = coin.instance()
	    money.connect("body_entered", self, "_on_Coin_body_entered")
	    money.position = Vector2(rand_range(25, 950), rand_range(25,450))
	    add_child(money) 

Coin:

extends Node2D

func _on_Coin_body_entered(body): 
    if body.is_in_group("Player"):
	   Player.score += 1
	   queue_free()

	

doorpen | 2021-09-17 05:48

Sorry, I can’t find anything wrong with your code. But it’s weird that your code extends Node2D when the nodes aren’t Node2D, right?

exuin | 2021-09-17 15:12

I tried just printing some text in the coin script which worked when I collected the first coin but didn’t work on the next coin so the function only runs on the first coin, would that be because I need to add connect(‘body_entered’, self, ‘on_Coin_body_entered’) to the function in the coin script

doorpen | 2021-09-18 09:06

I guess you can try that.

exuin | 2021-09-18 13:42