Invalid get index 'position' (on base: 'null instance').

Godot Version

help me please

Question

extends CharacterBody2D

var gravity = ProjectSettings.get_setting(“physics/2d/default_gravity”)

var chase = false
var speed = 50
@onready var anim = $AnimatedSprite2D
var alive = true

func _physics_process(delta):
# Add the gravity.
if not is_on_floor():
velocity.y += gravity * delta
var player = $“…/Player/player”
var direction = (player.position - self.position).normalized()
if alive == true:
if chase == true:
velocity.x = direction.x * speed
anim.play(“Walk”)
else:
velocity.x = 0
anim.play(“Idle”)
if direction.x < 0:
$AnimatedSprite2D.flip_h = true
else:
$AnimatedSprite2D.flip_h = false

move_and_slide()

func _on_detection_body_entered(body):
if body.name == “player”:
chase = true

func _on_detection_body_exited(body):
if body.name == “player”:
chase = false

func _on_death_body_entered(body):
if body.name == “player”:
body.velocity.y -= 200
death()

func _on_death_2_body_entered(body):
if body.name == “player”:
if alive == true:
body.health -= 2

func death ():
alive = false
anim.play(“Death”)
await anim.animation_finished
queue_free()

how do you get this nodepath string? from drag and drop?

1 Like

Thank you very much for your answer, but I already solved the problem