Invalid access to property or key 'global_position' on a base object of type 'Nil'. while getting a 3D location of an object

Godot Version

Godot Ver. 4.3

Question

Hello
I was trying to make a Following Player state using detection area. when I’m getting position for player that inside the area using global_position, the errors occurred.
why is that?

here’s the following state code

extends State
class_name FollowState

@export var enemy : CharacterBody3D
@export var move_speed := 1

var player = null
var chase = false

func Enter():
	chase = true
	
func Physics_Update(delta: float):
	if chase:
		var direction = player.global_position - enemy.global_position
		enemy.velocity = direction.normalized() * move_speed
		
	else:
		chase = false

func _on_detection_area_body_entered(body: Node3D) -> void:
	player = body
	chase = true

here’s my Finite State Machine code

class_name FiniteStateMachine
extends Node

@export var initial_state = CharacterBody3D

var current_state = State
var states : Dictionary = {}

func _ready() -> void:
	for child in get_children():
		if child is State:
			states[child.name] = child
			child.Transitioned.connect(on_child_transition)
	if initial_state:
		initial_state.Enter()
		current_state = initial_state

func _process(delta: float) -> void:
	if current_state:
		current_state.Update(delta)
	
func _physics_process(delta: float) -> void:
	if current_state:
		current_state.Physics_Update(delta)
		
func on_child_transition(state, new_state_name):
	if state != current_state:
		return
		
	var new_state = states.get(new_state_name.to_lower())
	if !new_state:
		return
		
	if current_state:
		current_state.exit()
		
	new_state.Enter()
	

What is a State? Where is Physics_Update called? Where do you access the global position? What line is the error triggered on?

OH, I’m so sorry… here’s my state code

class_name State
extends Node

signal Transitioned

func Enter():
	pass
	
func Exit():
	pass
	
func Update(_delta: float):
	pass

func Physics_Update(_delta: float) -> void:
	pass

the error triggered on global_position’s line…
I’m new to programming…
can you tell me how do I access the global position?

Your Enter function sets chase to true, but the player hasn’t been set so it’s probably still null. You can’t chase nothing :upside_down_face: