Invalid access to property or key 'global_position on base object of type 'Nil'

Godot Version

4.3

Question

So im trying to get my character to go to different waypoints and it is giving me an error saying in valid acces can someone help


const speed = 100
const ChaseSpeed = 700
@onready var nav_agent = $NavigationAgent2D
var waypoint = 1
var target = waypoint1
@export var waypoint1: Node2D
@export var waypoint2: Node2D
@export var waypoint3: Node2D
@export var waypoint4: Node2D
@export var waypoint5: Node2D
var agro = false
@export var Player: Node2D

# Called when the node enters the scene tree for the first time.
func _ready():
	pass
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
	var dir = to_local(nav_agent.get_next_path_position()).normalized() 
	if self.global_position == target.global_position:
		if waypoint == 5:
			waypoint = 1
		else:
			waypoint += 1
	
	if agro:
		target = Player
	elif waypoint == 1:
		target = waypoint1
	elif  waypoint == 2:
		target = waypoint2
	elif waypoint == 3:
		target = waypoint3
	elif waypoint == 4:
		target = waypoint4
	else:
		target = waypoint5

func makepath():
	nav_agent.target_position = target.global_position

func _on_timer_timeout():
	makepath()

Not sure if this will fix it but try adding @onready to var target = waypoint1.

1 Like

It did thank you i just had to add it to target instead

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.