How to check collision when using layers and mask

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

Hi I try to make a method globally that queue free the enemy and/or player when the life it’s 0. Problem is that i use an animated sprite with an area2d for player and enemies my script beeing attached to the sprite. The method is this:

Methods.gd

func die(area):
	area.queue_free()
	play_sound("Explosion")
	EventBus.emit_signal("instance_node",Global.Explosion,area.global_position)

but when the ememy need to die i get this:

Attempt to call function ‘queue_free’ in base ‘null instance’ on a
null instance.

what I’m thinking is that area is just null or it became null after they collide but I don’t know how to take a reference of what hits the enemy? Any idea

:bust_in_silhouette: Reply From: Tato64

That error means you are trying to delete something that doesnt exist, first of all check if you referenced it correctly (Wrong location or maybe a typo are the most common)

If the code is “correct”, then check if that node is not being deleted before by something else.

Still, it seems like we’re missing a lot of code to reach the cause… For example, where does this “area” var come from in the die(area) function?

By the way, to avoid problems, the player’s root node should be a KinematicBody or RigidBody not an AnimatedSprite