error- "invalid get index ‘position’ (on base: ‘viewport’)."

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

after writing code I got “invalid get index ‘position’ (on base: ‘viewport’).” this error
code -
extends KinematicBody2D

var motion = Vector2()
func _ready():
pass # Replace with function body.

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

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

move_and_collide(motion)
:bust_in_silhouette: Reply From: kidscancode

That error is because you’re trying to move the root viewport. When you write:

var Player = get_parent().get_node(".")

You’re getting the parent node - and then get_node(".") does nothing but still get the parent node. "." means yourself.

It seems this script is looking for the Player node. You haven’t shown your node setup, so I can’t tell you the correct path, but that is what you need to use.

For example, if the player is a sibling of this one, you might use

var Player = get_parent().get_node("Player")

which is the same as

var Player = get_node("../Player")