Signal from group not working

I’m trying to expand on a 2D game tutorial that I recently finished and I’ve run in to a big of a snag. I’ve created a Coin scene with the intention of grouping all instantiated coins in the active level scene and connecting a custom signal that emits when the Player touches the coin. The coin itself is

Node2d
          Area2D
                    CollisionShape2D
                    AnimatedSprite2D

I have added the coin scene to the group “coins” and grouped them in the level script. Here is the coin script:


signal coin_collected

func _on_area_2d_body_entered(body):
	if body is Player:
		coin_collected.emit()

And here is the relevant part of the level script:

func _ready():
        var coins = get_tree().get_nodes_in_group("coins")
	        print(coins)
	        for coin in coins:
		        coin.coin_collected.connect(_on_coin_collected)

func _on_coin_collected():
	print("Coin collected")

When I run the game I get an accurate print out of all the coins in the coins group, but when the Player touches the coin the _on_coin_collected function does not go off. I’m at a loss because I have a trap scene that is made of the same nodes and grouped in the same way that works as intended when the Player touches it.

Any insight would be beyond helpful.

Is everything in the same collision layer?

The only thing without further information I come up with:

  1. Signal on_area_2d_body_entered is not connected
  2. body is not Player

Try print(body) in on_area_2d_body_entered to get more info if your signal is indeed connected.

Oh my god I didn’t have it on the correct collision layer.

Thank you for pointing that out! I’ll never forget that again!

1 Like

While the problem turned out to be the collision layer, for future reference what other information could I provide that would be helpful? This is my first time seeking assistance and I would love to be more thorough next time.

Sorry, my phrasing was not very good.

You gave us good information imo. I didn’t mean to say you didn’t provide enough information. It just was my mind thinking, that I could find the problem easier having the project right in front of me.

I also forgot the possibility that the layers were not set accordingly. :smiley: