Why does player return null?

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

My code worked perfectly and something happened, now, I don’t know why, the player node returns null and all stop working.

extends KinematicBody2D

var motion = Vector2()

func _ready():
   pass

func _physics_process(delta):
  var player = get_parent().get_node("player")

  position += (player.position - position)/50

  look_at(player.position)

motion = move_and_collide(motion)

First of all calling var player = get_parent().get_node("player") in every physics frame is wrong. It’s enough to do this once in _ready().

Second, make sure your composed tree hierarchy is matching your query, use print_tree_pretty()to see.

sash-rc | 2021-08-20 07:35

:bust_in_silhouette: Reply From: MaybeAnonymous

Try this:

extends KinematicBody2D

var motion = Vector2()

var player = get_node(drag the player node inbetween these parentheses)

func _physics_process(delta):

  motion += (player.position - position)/50

  look_at(player.position)

motion = move_and_collide(motion)

Don’t make a variable that is constant be called every frame, also remember to have the correct path to your player node.