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