Invalid get index 'position' (on base: 'null instance'). I'm trying to get the enemy to chase the player.

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Dragonwarrior2004
extends KinematicBody2D

var max_health = 3
var health = max_health
var knockback = Vector2.ZERO
const SPEED = 100.0

func _physics_process(delta):
	knockback = knockback.move_toward(Vector2.ZERO, 200 * delta)
	knockback = move_and_slide(knockback)
	var direction = ($Player.position - position).normalize()
	var motion = direction * SPEED * delta
	position += motion

func _on_Hurtbox_area_entered(area):
	var knockback_vector = area.knockback_vector
	knockback = area.knockback_vector * 200
	health -= 1
	if health <= 0:

(Also, if I take out .position from $Player.position it helps that bug but causes another)

Edited: fixed your code formatting.

kidscancode | 2020-05-18 21:26

:bust_in_silhouette: Reply From: kidscancode

Your problem is that $Player doesn’t get a valid node.

$Player is equivalent to get_node("Player") which means “get a child of this node named ‘Player’”. Player is clearly not a child of this node.

You must use the actual path to the player node. I don’t know what that is because you haven’t shown what your scene setup is.

Misunderstanding node paths is a common beginner error. This may help: