This site is currently in read-only mode during migration to a new platform.
You cannot post questions, answers or comments, as they would be lost during the migration otherwise.
0 votes

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 physicsprocess(delta):
var Player = getparent().getnode("Player")

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

move_and_collide(motion)

func onArea2Dbodyentered(body):
if "Bullet" in body.name:
queue_free()

in Engine by (12 points)

2 Answers

0 votes

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.

by (22,191 points)
0 votes

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
by (3,906 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.