The Godot Q&A is currently undergoing maintenance!

Your ability to ask and answer questions is temporarily disabled. You can browse existing threads in read-only mode.

We are working on bringing this community platform back to its full functionality, stay tuned for updates. | Twitter

0 votes

I have 2 AudioStreamPlayer2D, one of which is in the main scene. I tryed to access both from a different scene using:

onready var game_music = get_node("/root/path_to_music")

The one in main scene caused me no problem, but the other one returned null!!!

Attempt to call function 'is_playing' in base 'null instance' on a
null instance.

How can I access AudioStreamPlayer2D in other scenes!? and does been a main scene or not have any thing to do with this problem?

Thank you in advance :-)

in Engine by (12 points)
recategorized by

1 Answer

0 votes

Here's the thing: "scenes" don't matter when the game is running. All you have is a bunch of nodes in the scene tree.

The error you're getting is because the path you have in that get_node() is returning null instance because it's invalid. You need to look at your scene tree and use the correct path to the node you want.

If you post a pic of your scene tree, I can probably show you exactly.

by (22,191 points)

Thank you for your answer..

I understand that this error could be generated from a wrong path, but i highly doubt it ...i have checked a million time!!

onready var game_music = get_node("/root/main/backgroundMusic")

This is my scene tree:
again your help is much appreciated.

What node is the script with that line on? onready is probably your problem. Children become ready first, so "main" is going to be ready last. Calling this in the ready of a node that's further down the tree will not work.

the on ready line is in a different scene in the same directory as all other scenes
i used the same line to access themeSong from main_menu scene and it worked but backgroundMusic loaded from main scene is causing problems
enter image description here

The "directory" has nothing to do with it. When you run the game, the nodes are all in the scene tree, and they become ready in reverse tree order. You can't call a node that's above during _ready().

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 Frequently asked questions and 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 [email protected] with your username.