Confused with variables

So i’m trying to make the hitbox of the enemy and in the scrip i use a variable called body so it can detect if it dies or damages the player by checking the body name. the problem is im using a code for another enemy.so in 1 code the “body” variable is working but in the other isnt
code that works:

@onready var game_manager: Node = %"Game Manager"
@onready var animated_sprite_2d: AnimatedSprite2D = $AnimatedSprite2D

# Called when the node enters the scene tree for the first time.
func _ready() -> void:
	pass # Replace with function body.


# Called every frame. 'delta' is the elapsed time since the previous frame.
@warning_ignore("unused_parameter")
func _process(delta: float) -> void:
	pass


func _on_area_2d_body_entered(body: Node2D) -> void:
	if(body.name =="Player"):
		var y_delta =position.y -body.position.y
		var x_delta = body.position.x - position.x
		if (y_delta >40):
			animated_sprite_2d.animation = "morte"
			body.jump()
			queue_free()
		else:
			print("decrease player health")
			game_manager.decrease_health()
			


code that does not work[it was copied]:

...
func _on_area_2d_area_entered(area: Area2D) -> void:
	if(body.name =="Player"):
		var y_delta =position.y -body.position.y
		var x_delta = body.position.x - position.x
		if (y_delta >40):
			animated_sprite.animation = "hurt"
			body.jump()
			queue_free()
		else:
			print("decrease player health")
			game_manager.decrease_health()

can some1 explain to my why isnt it working :sob:

Hi,

You’ve copied the code block but not the function parameter name area:

_on_area_2d_area_entered(area: Area2D)

You should use area as the variable name inside your function, like this:

if (area.name =="Player"):
    var y_delta = position.y - area.position.y
	var x_delta = area.position.x - position.x
	if (y_delta > 40):
		animated_sprite.animation = "hurt"
		area.jump()
		queue_free()
	else:
		print("decrease player health")
		game_manager.decrease_health()
1 Like