Variable's value not changing after update

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

I am currently making a simple, enemy script for a 3D dungeon game. I made an area node which prints “enemy hit” when colliding with the enemy (Area3D node as well). I added a script to the enemy area3D node which would print the enemy’s health (initial value is 10) in the console and once the variable’s value got to 0, it would delete itself. My problem is that the variable “health” is not changing after updates.

Here is my code:

Player script (I took out the movement script here)

extends CharacterBody3D



@onready var camera = $Camera3D
@onready var anim_player = $AnimationPlayer
@onready var hitbox = $Camera3D/WeaponPivot/WeaponMesh/Hitbox

if Input.is_action_just_pressed("attack"):
	anim_player.play("attack")
	hitbox.monitoring = true

func _on_animation_player_animation_finished(anim_name):
if anim_name == "attack":
	anim_player.play("idle")
	hitbox.monitoring = false

func _on_hitbox_area_entered(area):
if area.is_in_group("enemy"):
	print("enemy hit")
	

Enemy script

extends Area3D

var health = 10

func _ready():
pass

func _process(delta):
pass

func _on_enemy_area_entered(area):
if area.is_in_group("weapon"):
	health -= 1
	print(health)
if health == 0:
	print("defeated")
	queue_free()

When I check console after hitting the enemy, it registers and prints “enemy hit” but does not update the variable’s value from 10 to 9, or 8, or 7, and so on. Can anyone help me?

Does the _on_enemy_area_entered ever get triggered? If not, does the enemy Area2D have monitoring enabled? And is the weapon Area2D in the correct group?

a_world_of_madness | 2023-06-11 09:57