Enemies all dying when reaching 0 health instead of just one

Godot Version

4.6.1

Question

I’ve been trying to make enemies in my game but everytime the bullet hits them and their health reaches 0, they all get deleted instead of just one getting deleted. Could someone tell me how to fix this?

Enemy script

extends ColorRect

@onready var health: int = 100
@onready var damage: int = 20

func _ready():
	Tracker.connect("hit", dealDamage)

func dealDamage():
	health -= damage
	if health < 0:
		health = 0
	if health == 0:
		queue_free()

Bullet script

extends ColorRect

const SPEED: int = 800

func _process(delta: float):
position.x += SPEED * delta

func _on_visible_on_screen_notifier_2d_screen_exited():
queue_free()

func _on_area_2d_body_entered(body: Node2D):
Tracker.emit_signal(“hit”)
queue_free()

You’re using an autoload to transmit the hit signal that all your enemies connect to. So whenever a hit happens, all enemies take damage.
You shouldn’t use autoload for that, but rather take advantage of the body argument inside the _on_area_2d_body_entered callback to damage only specific enemy.

4 Likes

Yea that fixed it thanks

1 Like