Get_node can't find relative node

Godot Version

4.2.2

Question

I am trying to create a relative reference from raycast which is the child of the Player (kinematic body2d). This way no matter what scene the Player is brought into the raycast can connect to the UI element which is also a child of the Player.
@onready var interaction_label = get_parent().get_node(“/Camera/UI/InteractionLabel”). the error comes back Node not found.
The Camera is an instanced scene on the Player which is an instanced scene in the world. So the current global location is /root/Office/Player/Camera/UI/InteractionLabel,
but I need it to be dynamic and also work for /root/Bathroom/Player/Camera/UI/InteractionLabel when the scene changes. I also don’t have any autoloads.

Be aware that the node may not be in the tree yet. There is a ready order. It starts from the bottom leaves and goes up. If the ray node is below the label it will probably fail.

1 Like

I would have a ray emit a signal and have the label listen to that signal.

Yea, I checked the order and the raycast should come after the UI. Everything works with an absolute reference, but ill probably switch to emitting a signal since that will keep other problems from arising and probably lighten the startup load. Thanks!

This is not a relative path, the initial / makes it an absolute path, try removing the initial forward slash

@onready var interaction_label = get_parent().get_node("Camera/UI/InteractionLabel")
# or
@onready var interaction_label = get_node("../Camera/UI/InteractionLabel")

Or using @export and selecting the node from the inspector

Thanks! I figured it had to be a stupid syntax issue.

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