"Invalid get index '0' (on base: 'Nil')" when enemy spawns

Godot Version

4.1.3

Question

I’ve got an enemy spawning into the world when I pick up an object that only moves when it can’t be seen (similar to the Weeping Angels in Doctor Who) and I’ve done a little bit of ray-casting to check to see if there is anything blocking the player’s line of sight to the enemy. When the enemy spawns in, the game crashes and I get the following error with the following line in the enemy script being highlighted:

Invalid get index ‘0’ (on base: ‘Nil’)

func calculate_distance(target_position, origin_position):
	return pow(((target_position[0]-origin_position[0])+(target_position[2]-origin_position[2])), 0.5)

The code used (see below) is the exact same code I used in another project that I did to learn my way around this engine so the issue should be to do with the enemy being spawned in

extends CharacterBody3D

@onready var nav_agent = $NavigationAgent3D

@onready var Player = $"../Player"
@onready var GUI = $"../GUI"

var has_caught = false

var SPEED = 1.0 #we can change this if it is too fast for what we want

var collision
var rayCast_position
var Player_position
var isMonsterVisible = false


func _physics_process(delta):
	
	if (isMonsterVisible == true and collision != "Monster") or isMonsterVisible == false:
		if calculate_distance(rayCast_position, Player_position) <= calculate_distance(self.position, Player_position):
			SPEED = 2.0
			
	else:
		SPEED = 0.0
	
	
	var current_location = global_transform.origin
	var next_location = nav_agent.get_next_path_position()
	var new_velocity = (next_location - current_location).normalized() * SPEED
	
	velocity = new_velocity
	move_and_slide()

func update_target_location(target_location):
	nav_agent.target_position = target_location

func _on_navigation_agent_3d_target_reached():
	has_caught = !has_caught
	if has_caught == true:
		print("death")
		print(has_caught)
		GUI._on_death()


func _on_visible_on_screen_notifier_3d_screen_entered():
	print("Visible")
	isMonsterVisible = true
	#SPEED = 0


func _on_visible_on_screen_notifier_3d_screen_exited():
	print("Not visible")
	isMonsterVisible = true
	#SPEED = 1.0


func _on_player_on_collide(collider, raycast_position, origin_position):
	#print(collider)
	
	collision = collider
	rayCast_position = raycast_position
	Player_position = origin_position
	
	pass # Replace with function body.

func calculate_distance(target_position, origin_position):
	return pow(((target_position[0]-origin_position[0])+(target_position[2]-origin_position[2])), 0.5)



#func _on_navigation_agent_3d_target_reached():
	#print ("in range")

The variable Player_position is not set before _physics_process is called.