How to delete instances?

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

I have lots of instances of a node that I want to delete themselves when they enter an area.

The initial node that I have on the screen deletes itself fine but its instances do not (even though they are all being detected by the area). I’ve tried lots of stuff from the forum but I can’t figure it out.

In my code it’s known as sugar, and the area is known as bin. I’ve tried things like sugar_instance.instance().queue_free() but i can’t get it to work. Thanks in advance.

Main

Extends Node2D

onready var sugar_instance = preload("res://sugar.tscn")

func _process(delta):

add_child(sugar_instance.instance())

sugar

func _on_bin_area_body_entered(body):
	
	print("Entering bin")
	queue_free()
:bust_in_silhouette: Reply From: Dlean Jeans

Have you tried

body.queue_free()

Works perfectly, thanks so much.

Can I ask what this does and why this works? And why queue_free() doesn’t?

Cheers

mangalala | 2019-07-17 12:26

Just queue_free() will free the node attached to the script, in this case - bin_area. But body.queue_free() will free the body entering the bin_area. You simply called queue_free() on the wrong node.

Dlean Jeans | 2019-07-17 12:30