i keep getting this eror message position (on base : ' null instance')

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

im making a tod down shooter game and this is the eror i get in the enemy script

extends KinematicBody2D

var motion = Vector2()

func _ready():
pass # Replace with function body.

func _physics_process(delta):
var Player = get_parent().get_node(“Player”)

position += (Player.position - position)/50
look_at(Player.position)

move_and_collide(motion)

func _on_Area2D_body_entered(body):
if “Bullet” in body.name:
queue_free()

:bust_in_silhouette: Reply From: kidscancode

First of all, that’s not the complete error message. In future, please include the full error.

“null instance” is what you get when you try to get_node() a node that doesn’t exist. That means that get_parent().get_node("Player") is not a valid node path. Check your scene tree again and use the correct path from the enemy to the player node.

:bust_in_silhouette: Reply From: timothybrentwood

This is happening because get_parent().get_node("Player") is returning null because Godot can’t find a node at that path with the name Player at the time it searches for it. It could be that your Player is being queue_free()'d while the game is running or there simply doesn’t exist a Player node at the path you gave it.

func _physics_process(delta):
    var Player = getparent().getnode("Player")
    if is_instance_valid(Player):
        position += (Player.position - position)/50
        look_at(Player.position)

    move_and_collide(motion)

That code will eliminate the crashes but may not give you the behavior you desire. Your code will only work if your node structure is like this:

-SomeMutualParentNode
--TheNodeTheScriptYouPostedIsAttachedTo
--Player