Game works for about 2 seconds, then throws error and crashes

Godot Version

Godot 4.3

Question

extends CharacterBody2D

@export var speed = 5
var player_position
var target_position
@onready var player = get_node(“…/player”)

func _physics_process(_delta):
player_position = player.position
target_position = (player_position - position).normalized()

if position.distance_to(player_position) > 0:
	move_and_collide(target_position * speed)
	look_at(player_position)

this is code for my enemy. i am afraid it is not very good, but it should be working though. for some reason, it works for a few seconds, and then throws error : Invalid get index ‘position’ (on base: ‘null instance’).
it also gives another error - E 0:00:01:0728 enemy.gd:6 @ _ready(): Node not found: “player” (relative to “/root/game/enemy”).
this will most likely be a noob q, but please help me. this game is a part of a game jam and has to be submitted in one days time.

Can you show me the SceneTree?
I believe the player node might actually be called differently.
maybe it’s called “Player”.

sure…
game
→ player
—> sprite 2d
—> collision shape 2d
—> flail
-----> sprite 2d
-----> collision shape 2d
→ camera 2d
→ enemy_spawner
→ enemy
do let me know if you need the script for the enemy spawner pr that of the player

the flail is supposed to rotate around the player, though I’m afraid that’s broken too

What happens if you write at the top of the _physics_process() method:
if player == null: return

Does the enemy just stand still or will it start moving with a delay?

also try to print out during the enemy’s ready method just to see if your onready is kinda not onready enough for the script.
func _ready() -> void: print(player)

I am not sure about the “the game works for about 2 seconds”, you might need to place Breakpoints and see the exact point of error.

Also is it possible to have a screenshot of your SceneTree? This makes me also take note of the different object types.

i tried the null return statement and now the game does not immediately crash, although it does still give me the same error in the debugger. weirdly tho, when the enemy collides with the player, the enemy (sometimes but not always) glitches up and down a few times, before zooming off upwards. i tried printing the enemy position in the console, and both the x and y values become larger and larger(or rather, smaller and smaller because the values are negative). any idea why?

Does your player ever get deleted?

I believe there is something wrong with the way how your enemy references the player.
Could I ask you to try out this:
@onready var player = get_node("/root/game/player")

If if this not working, please try this

func _ready() -> void:
   var test = get_node(“…/player”)
   print(test == null)

weirdly tho, when the enemy collides with the player, the enemy (sometimes but not always) glitches up and down a few times, before zooming off upwards. i tried printing the enemy position in the console, and both the x and y values become larger and larger(or rather, smaller and smaller because the values are negative). any idea why?

Most likely a velocity issue which happens when the player is there but not detected by the enemy script. Let’s focus on having the player variable not be null while the game is running.