Multiple instances of the same enemy scene sharing state / breaking each other

Godot Version

Replace this line with your Godot version

Question

Hello!

​I’m running into an issue in Godot where placing two of the exact same enemy scene into my level causes strange behavior (like them dying together, sharing health, or interfering with each other’s targeting).

​The Issue: When there is only one enemy, it works fine. When I add a second one, they conflict.

​Here is my enemy script:

extends CharacterBody2D

signal disable_collisons

signal disable_the_attack

var cool_down = false

var is_attacking = false

var dead = false

var is_punch = false

var health = 50

var SPEED = 300.0

var JUMP_VELOCITY = -400.0

var do_noises = true

var is_punching = false

var knockback_velocity: Vector2 = Vector2.ZERO

enum State { MOVE, LUNGE, KNOCKBACK }

var state = State.MOVE

var knockback = Vector2.ZERO

func _physics_process(delta: float) → void:

match state:

	State.KNOCKBACK:

		velocity.y += get_gravity().y \* delta \* 0.3

		velocity.x = knockback.x

		knockback.x = move_toward(knockback.x, 0, 800 \* delta)

		if abs(knockback.x) < 10:

			velocity.x = 0

			state = State.MOVE

		if knockback.length() < 10:

			state = State.MOVE

	State.LUNGE:

		\# set lunge velocity here, then go back to MOVE when done

		pass

move_and_slide()

if not is_on_floor():

	velocity.y += get_gravity().y \* delta \* 0.3

if knockback.length() < 10:

	velocity.x = 0

	state = State.MOVE 

if is_punch == false:

	if is_punching == false:

		if dead == false:

			if is_attacking == false:

				if velocity.x == 0:

					$"nature taken knight shape".visible = true

					$"nature taken knight shape 2".visible = false

					$"green shoooooooot".visible = false

					$"green shoot other side".visible = false

					$"AnimationPlayer of nature taken knight".play("nature taken knight idle animation")

					$head.visible = false

					$"left hand".visible = false

					$"right hand".visible = false

					$tourso.visible = false

if velocity.x != 0:

	pass

else:

	velocity.x = move_toward(velocity.x, 0, SPEED)

move_and_slide()

func _on_left_attack_get_ddemge() → void:

var dir = sign(global_position.x - $"../yafa".global_position.x)

knockback = Vector2(dir \* -300, 0)

state = State.KNOCKBACK

health -= 10

$"robot geting damged".play()

$"nature taken knight shape".visible = false

$"nature taken knight shape 2".visible = false

$"green shoooooooot".visible = false

$"green shoot other side".visible = false

$head.visible = true

$"left hand".visible = true

$"right hand".visible = true

$tourso.visible = true

$"AnimationPlayer of nature taken knight".play("damged")

if health <= 0:

	dead = true

	print("dead")

	$"nature taken knight shape".visible = false

	$"nature taken knight shape 2".visible = false

	$"green shoooooooot".visible = false

	$"green shoot other side".visible = false

	$head.visible = true

	$"left hand".visible = true

	$"right hand".visible = true

	$tourso.visible = true

	$"AnimationPlayer of nature taken knight".play("deathy after")

	disable_collisons.emit()

	$"nature taken knight noises".stop()

	await get_tree().create_timer(1).timeout

	queue_free()

func _on_right_attack_get_damged() → void:

var dir = sign(global_position.x - $"../yafa".global_position.x)

knockback = Vector2(dir \* 300, 0)

state = State.KNOCKBACK

health -= 10

$"robot geting damged".play()

$"nature taken knight shape".visible = false

$"nature taken knight shape 2".visible = false

$head.visible = true

$"left hand".visible = true

$"right hand".visible = true

$tourso.visible = true

$"AnimationPlayer of nature taken knight".play("damged")

if health <= 0:

	dead = true

	print("dead")

	$"nature taken knight shape".visible = false

	$"nature taken knight shape 2".visible = false

	$head.visible = true

	$"left hand".visible = true

	$"right hand".visible = true

	$tourso.visible = true

	$"AnimationPlayer of nature taken knight".play("deathy after")

	disable_collisons.emit()

	$"nature taken knight noises".stop()

	await get_tree().create_timer(0.5).timeout

	queue_free()

func _on_left_go_left() → void:

is_attacking = true

$head.visible = false

$"left hand".visible = false

$"right hand".visible = false

$tourso.visible = false

$"nature taken knight shape".visible = false

$"nature taken knight shape 2".visible = false

$"green shoooooooot".visible = false

$"green shoot other side".visible = true

$"AnimationPlayer of nature taken knight".play("green shot animation lft other side")

$plant.play()

await get_tree().create_timer(0.3).timeout

is_attacking = false

func _on_right_play_right() → void:

is_attacking = true

$head.visible = false

$"left hand".visible = false

$"right hand".visible = false

$tourso.visible = false

$"nature taken knight shape".visible = false

$"nature taken knight shape 2".visible = false

$"green shoooooooot".visible = true

$"green shoot other side".visible = false

$"AnimationPlayer of nature taken knight".play("green shhhot animaiton")

$plant.play()

await get_tree().create_timer(0.3).timeout

is_attacking = false

func _on_punch_right_punch_right() → void:

if is_punch == false:

	is_punching = true

	print("i am punching you!")

	$"nature taken knight shape".visible = false

	$"nature taken knight shape 2".visible = false

	$"green shoooooooot".visible = false

	$"green shoot other side".visible = false

	$head.visible = true

	$"left hand".visible = true

	$"right hand".visible = true

	$tourso.visible = true

	$head.flip_h = false

	$"left hand".flip_h = false

	$"right hand".flip_h = false

	$tourso.flip_h = false

	$"AnimationPlayer of nature taken knight".play("punch animation")

	await $"AnimationPlayer of nature taken knight".animation_finished

	is_punching = false

func _on_punch_left_punch_left() → void:

if is_punching == false:

	is_punch = true

	print("i am punching you!")

	$"nature taken knight shape".visible = false

	$"nature taken knight shape 2".visible = false

	$"green shoooooooot".visible = false

	$"green shoot other side".visible = false

	$head.visible = true

	$"left hand".visible = true

	$"right hand".visible = true

	$tourso.visible = true

	$"AnimationPlayer of nature taken knight".play("punch anim")

	await $"AnimationPlayer of nature taken knight".animation_finished

	is_punch = false

func _on_lunch_right_lunch_right() → void:

if randf() < 0.5:

	print(" i go -")

	var dir = sign(global_position.x - $"../yafa".global_position.x)

	knockback = Vector2(dir \* -400, 0)

	state = State.KNOCKBACK

else:

	print("i go ")

	var dir = sign(global_position.x - $"../yafa".global_position.x)

	knockback = Vector2(dir \* 400, 0)

	state = State.KNOCKBACK

func _on_lunch_left_lunch_left() → void:

if randf() < 0.5:

	print(" i go -")

	var dir = sign(global_position.x - $"../yafa".global_position.x)

	knockback = Vector2(dir \* -400, 0)

	state = State.KNOCKBACK

else:

	print(" i go ")

	var dir = sign(global_position.x - $"../yafa".global_position.x)

	knockback = Vector2(dir \* 400, 0)

	state = State.KNOCKBACK

Please format your code properly, and please replace that line with your godot version that the base help template gave you.

Its a 2d game and my godot version is 4.7

here is the code:

Please read the post I gave you. It tells you how to format your code properly, and specifically tells you not to post screenshots or videos of your code.

You have probably copied instead of duplicated the node.

Also, in the future dont just spam in full pages of random code like this. You will be less likely to get help as no one wants to read through something like that, especially when not formatted.

but what is the solution for my problem i stilll fun in the problme of when i put more than 1 enymy it gets coas and wrong and mixed signals?

this what happens when i put 2 enimes i am sorry if this looks confusing because i didnt show everything but help me fix it help me to fix any issue like that n the future

You took no time to actually read the post I gave you, and you continue to ignore requests to edit your post and properly format your code so people can actually help you out, and now you basically made a demand that we should help you with any issues you have.
I personally don’t think this approach will get you any help. People here are volunteers, they give their free time to try and help others. it’s not tech support. Please take the time to read through the post I gave you and follow its advice first.

i didnt understand anything of what i do worng when posting i didnt ignore i jsut didnt understand

Yeah, you have bugs in your code or setup. You need to debug it.

I am asking for help because i dont know how to solve the problem in the correct way

You need to present your problem in the best possible way, providing clear and comprehensive information about it. Otherwise noone will understand what the problem actually is. Carefully read the guidelines @tibaverus posted and re-formulate the presentation of your problem accordingly.

For start, format your code properly. Whitespace is a part of GDScript syntax. If the formatting gets broken - your code becomes undecipherable.