What am I doing wrong here?

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

I made a coin scene and when the player collide with it the coin disappears and the score goes higher but it’s not working.

here is the coin code:

extends Area2D

signal e_grabbed

func _on_coin_body_entered(body):
	if body.name == "player":
		emit_signal("e_grabbed")
	queue_free()

and this is the main scene code:

func spawn():
	var coin_scene = load("res://scenes/coin.tscn")
	randomize()
	var coin = coin_scene.instance()
	var random = Vector2(rand_range(30,230),-4)
	coin.set_position(random)
	coin.connect("e_grabbed", self, "_on_e_grabbed")
	$coins.add_child(coin)
	
func _on_e_grabbed():
	print("grgrg")

it doesn’t print ‘grgrg’

Did you try adding a debugger to the _on_coin_body_entered(body) function and checking if the body entering is actually what you expect it to be? For example, you might have a player but the name is not matching ‘player’.

tastyshrimp | 2019-12-19 22:12

:bust_in_silhouette: Reply From: System_Error

Ah, I see an issue. In your Coin.gd, you left queue_free() outside of the if loop.

extends Area2D

signal e_grabbed

func _on_coin_body_entered(body):
	if body.name == "player":
		emit_signal("e_grabbed")
		queue_free()

That’s all I would do. If the system fusses, try putting the Player scene into a Group called “player”, and change if body.name == "player" to if body.is_in_group("player").

In mainscene.gd, I’d put randomize() into the _ready() function, and have var coin_scene = preload("res://scenes/coin.tscn") between the topmost function, and the extends line.

Apologies if this is a bit of a mess.