How to get refrence to a player

Godot Version

Godot_V.4.2.2

Question

I have a enemy and a player,and i want to enemy to follow player,so i use that position that enemy has to follow is = player.positon but i always get error Invald get index ‘direction’ and i think my problem is a refernce to player that i am using.Can somone help

@onready var player = get_parent().get_parent().get_node(“main/Player”)
i am using this reference

many ways to do it, one simplest way is to set a group to the player example
“player”
image

then when you need to get the player node, you can just

var player = get_tree().get_first_node_in_group("player")
1 Like

if this works i am going to scream

it says cannot call method ‘get_first_node_in_group’ on a null value

try not put it on that @onready line
instead, try
it on _ready()

var player
func _ready():
	player = get_tree().get_first_node_in_group("player")

it really works man!Thanks bro i was looking for solution for 3 days.Thank you so much man

1 Like

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