How do I destroy an object when the play collides with it.

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

I am brand new to Godot and GDScript. I’m trying to make an item disappear when the player collides with it. Kinda like a Coin or Item Box.

This is for a 2D platformer game project just for me learn. Here is the code snippet:

func _on_coin_area_shape_entered(area_rid, area, area_shape_index, local_shape_index):
	$Coin.queue_free()

I hooked up the signal from the Coin to the player. Am I Missing something or doing something wrong entirely?

Any help is great. But keep in mind I’m a COMPLETE NOOB in Godot.

:bust_in_silhouette: Reply From: zhyrin

What is this script attached to, the player?
So the player listens to the coin signaling something entered its shape, and then the player deletes the coin?
Why not have the coin delete itself when something enters its shape?

Why do you use the area_shape_entered signal? I personally never used that one, area_entered and body_entered seem more useful.
By the way, what is the coin supposed to collide with, an area or a (collision) shape? Maybe you are connected to the wrong signal.

Finally to your last question: if this is the player’s script, the second line assumes the player has a child node named Coin. It’s not necesserily wrong, but the relationship is weird. If it’s a child node, it denotes that the player owns the coin, while in this case I assume you want the player and coin be separate objects in your world right?

As I know that when i duplicate coin node (ctrl+d) it gets from
“coin”
to:
“coin2”
Thats why I think this code works for me good for players ($Player) root script that can be called from “World.gd” or as you had called it as Node2D script:

for index in $Player.get_slide_count():
	var collission = $Player.get_slide_collision(index)
	if collission.collider.name.begins_with("coin"):
		get_node(collission.collider.name).queue_free()