Deleting bullets

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

When the hero script receives signal body entered I want to delete the bullet which is instance. I tested now it’s showing this Attempt to call function ‘queue_free’ in base ‘previously freed instance’ on a null instance.

func _on_Area2D_body_entered(body):
		if body.name == "zombiGirl":
			#print("zombi touched")
			
				delete_Magic()
func delete_Magic():
	magic_copy.queue_free()

someone told me I should use call deferred I don’t know how to use it

:bust_in_silhouette: Reply From: rossunger

"Attempt to call function ‘queue_free’ in base 'previously freed instance " means the bullet has already been deleted. Is there some other place in the script where you delete it?

if magic_copy the bullet that you’re trying to delete?
One simple fix is to just check
if is_instance_valid(magic_copy): magic_copy.queue_free()

call_deferred has the following syntax:
call_deferred("NameOfTheFunctionToCall", "ParameterToPassToTheFunction")
and what it does is it calls that function a bit later… like, after all the other code for that frame has been executed.