Enemies spawning on scene there not being instantiated on

Godot 4

please help I’ve posted before on this but no ones helping, it was fine until I made a tile map then deleted it because it was showing on a separate scene for some reason and now I can’t stop the enemy from spawning on the wrong scene. Enemy 2 is spawning on Enemy 1’s scene.

scene 1 code:
extends Node
@export var mob_scene: PackedScene
func _ready():
$Area2D.start($Marker2D.position)
$Timer2.start()
randomize()

func _process(delta):
pass

func _on_timer_2_timeout():
$Timer4.start()
$Timer3.start()

func _on_timer_4_timeout():
var mob = mob_scene.instantiate()
var mob_spawn_location = $Path2D/PathFollow2D
var direction = mob_spawn_location.rotation + PI /2
mob.position = mob_spawn_location.position
direction += randf_range(-PI / 4, PI / 4)
mob.rotation = direction
var velocity = Vector2(randf_range(150.0, 250.0), 0.0)
mob.linear_velocity = velocity.rotated(direction)
add_child(mob)

func _on_timer_3_timeout():
get_tree().change_scene_to_file(“res://mimicdefeated.tscn”)

enemy 1 code:
extends RigidBody2D

func _on_visible_on_screen_notifier_2d_screen_exited():
queue_free()

scene 2 code:

extends Node
@export var mob_scene2: PackedScene
func _ready():
$Timer5.start()
randomize()

func _on_timer_5_timeout():
$Timer6.start()

func _on_timer_6_timeout():
var mob_spawn_location2 = Vector2(600, randf_range(-450 , 450))
var mob2 = mob_scene2.instantiate()
var direction = 0
mob2.rotation = direction
mob2.position = Vector2(600, randf_range(-450 , 450))
var velocity2 = Vector2(randf_range(-150.0, -250.0), 0.0)
mob2.linear_velocity = velocity2.rotated(0)
add_child(mob2)

enemy 2 code:

extends RigidBody2D
func _enter_tree():
$AnimatedSprite2D.play(“run”)

func _on_visible_on_screen_notifier_2d_screen_exited():
queue_free()

You may have already tried some of these, but this is what I can see based on the code:

  • In the _on_timer_4_timeout() function in Scene 1, you’re instantiating mob_scene but not checking if it’s valid. Make sure mob_scene is set to the correct scene.
  • In Scene 2, in the _on_timer_6_timeout() function, you’re setting the position of mob2 to (600, randf_range(-450 , 450)) but also setting mob_spawn_location2 to (600, randf_range(-450 , 450)). You should use only one of these for setting the position.
  • Ensure that the scenes referenced by mob_scene and mob_scene2 are indeed the correct enemy scenes.
  • Check if there are any leftover references to the deleted tile map that might be affecting the spawning of enemies.

I can’t read unformatted code. Please put it in in a code block.

```
#Code

```

1 Like

How can I check for the references to the deleted tile map?

I fixed it, all I did was delete the scene then rebuild it. I still don’t really know what the issue was but it’s fixed none the less.

Nvm, apparently at some point (not sure when) I had attached a node2D underneath the player scene