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?

in Engine by (48 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.

by (27,902 points)
selected by
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.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.