I've got this weird error that states "Attempt to call function 'look_at' in base 'null instance' on a null instance"

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

onready var pivot: Position3D = $Pivot

export var air_friction = 2
export var friction = 9
export var speed := 10.0
export var jump_strength := 20.0
export var gravity := 40
export var acel = 70
var _velocity := Vector3.ZERO
var _snap_vector := Vector3.DOWN
var control_sens = 0.8
var move_direction = Vector3.ZERO

func _ready():

func _physics_process(delta):

	move_direction.x = Input.get_action_strength("move_right") - Input.get_action_strength("move_left")
	move_direction.z = Input.get_action_strength("move_backward") - Input.get_action_strength("move_forward")
	var direction = get_direction(move_direction)
	apply_movement(move_direction, direction, delta)
	_velocity.y -= gravity * delta
	var just_landed = is_on_floor() and _snap_vector == Vector3.ZERO
	var is_jumping = is_on_floor() and Input.is_action_just_pressed("jump")
	if is_jumping:
		_velocity.y = jump_strength
		_snap_vector = Vector3.ZERO
	elif just_landed:
		_snap_vector = Vector3.DOWN
	_velocity = move_and_slide_with_snap(_velocity, _snap_vector, Vector3.UP, true)
	apply_friction(direction, delta)
func get_direction(move_direction):
	var direction = (move_direction.x * CamScript.transform.basis.x) + (move_direction.z * CamScript.transform.basis.z)
	return direction

func _process(delta):

func apply_movement(move_direction, direction, delta):
	if direction != Vector3.ZERO:
		_velocity.x = _velocity.move_toward(direction * speed, acel * delta).x
		_velocity.z = _velocity.move_toward(direction * speed, acel * delta).z
		pivot.look_at(global_transform.origin + direction, Vector3.UP)
func apply_friction(direction, delta):
	if direction == Vector3.ZERO:
		if is_on_floor():
			_velocity = _velocity.move_toward(Vector3.ZERO, friction * delta)
			_velocity.x = _velocity.move_toward(direction * speed, air_friction * delta).x
			_velocity.z = _velocity.move_toward(direction * speed, air_friction * delta).z

screenshot of scene

:bust_in_silhouette: Reply From: Wakatta


This can mean only one thing your pivot var has not been properly set when you try to access it


if pivot:
    pivot.look_at(global_transform.origin + direction, Vector3.UP)

10 Cents Advice

You actually don’t need a node for this and can offset that position from the transform

var pivot = global_transform.basis.z + offset