I delete an enemy when its health reaches zero but the areas2ds script is still active and gives this error

Godot Version

4.5

Question

This is a script for my enemy damage indicator when you jump on it’s head you don’t take damage the handle danger function controls that the problem is that when its current health hits zero i get this error: “Invalid access to property or key ‘currenthealth’ on a base object of type ‘previously freed’.”

extends Area2D


func _on_body_entered(body: Node2D) -> void:
	if body is PlayerController:
		Bull.currenthealth -= 1
		body.handle_danger(0,0.5)
		print("off")
		return
extends Node2D

class_name Bull_1

@onready var animated_sprite_2d: AnimatedSprite2D = $AnimatedSprite2D
@onready var currenthealth: int = maxhealth

@export var Speed = 40.0
@export var maxhealth = 3

var direction = -1.0

# 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.
func _process(delta: float) -> void:
	position.x += direction * Speed * delta
	print("Bull: ", currenthealth)
	if currenthealth <= 0:
		_death()



func _on_timer_timeout() -> void:
	direction *= -1
	animated_sprite_2d.flip_h = !animated_sprite_2d.flip_h


func _death() -> void:
	if currenthealth <= 0:
		var tween = create_tween()
		
		tween.tween_callback(self.queue_free)

you see it deletes itself

Does it delete the area as well?

Which script line causes the error?

it doesn’t delete the area the first script causes the error

Should the area be deleted?

i have an enemy and an area for enemy damage that is each in a separate scene i trying to make it when its health = 0 that the bull disappears and no longer has an effect so yes the area as well

Both should be part of the same scene then, with area being the top node in the scene. Let it handle the self-deletion.

1 Like

what script should i use to make it delete the entire entity

The script in entity’s top node.