How to access an AudioStreamPlayer2D from a different scene?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By u_nes

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 :slight_smile:

:bust_in_silhouette: Reply From: kidscancode

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.

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.

u_nes | 2020-05-02 00:27

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.

kidscancode | 2020-05-02 00:36

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

u_nes | 2020-05-02 00:44

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().

kidscancode | 2020-05-02 01:05