0 votes

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?

asked Dec 15, 2017 in Engine by haxpor (38 points)

1 Answer

0 votes
Best answer

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.

answered Dec 15, 2017 by Zylann (26,149 points)
selected Dec 16, 2017 by haxpor
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.