How to get a node within the same scene from CanvasLayer node

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By haxpor
:warning: Old Version Published before Godot 3 was released.

I have the following node structure inside main scene.

enter image description here

I’m trying to get SamplePlayer2D node from play_fruit node. The latter is a child of CanvasLayer according to doc would mean this node seems to be at the same level of Node2D within this scene.

Inside play_fruit node’s script, I have this code to get the node.

onready var test = get_node("/root/main/Node2D/SamplePlayer2D")

but I always get empty or null every time.

What’s the correct way to do this?

:bust_in_silhouette: Reply From: Zylann

Maybe this instead:

onready var test = get_node("/root/Node2D/SamplePlayer2D")

You can also use relative path instead:

onready var test = get_node("../../SamplePlayer2D")

Suggestion: if your sound is hanging there in the main scene and not associated with an object in the world (like a GUI sound), you can use SamplePlayer (without the 2D) which you can put in your UI so you get access to it where you actually need it.