Parent not finding child

Godot Version

Godot 4.3-stable

Question

AudioStreamPlayer2D not working correctly as a child of my coin class

Hi, I have encountered a problem while making my coin class, more specifically trying to add the AudioStreamPlayer2D to it. it works fine until it reaches the pickupsound.play() line, when the sound is supposed to play. I debugged a little, and added this line :print(“Children of Coin:”, get_children()) and it outputs Children of Coin:[AnimatedSprite2D:<AnimatedSprite2D#35970352688>, CollisionShape2D:<CollisionShape2D#35987129905>] in the console, meaning it does not recognize my pickupSound at all, but it does for the other components. I will add an image of my screen in the comments. Thank you in advance!

Can you show the error messages in the Debugger-panel?

My guess is you didn’t assign coin.wav to the pickupSound AudioStreamPlayer.

yes :E 0:00:05:0234 coin.gd:6 @ _ready(): Node not found: “pickupSound” (relative to “/root/Game/Coins/Coin”).
<C++ Error> Method/function failed. Returning: nullptr
<C++ Source> scene/main/node.cpp:1792 @ get_node()
coin.gd:6 @ _ready()

i think you use queue_free and the sound is terminate and its nullptr

add autoload name him as “sound_handler” and put the sound in there
create function in the sound handler like this

func playPickUpSound(pos):
    %sound.position=pos
    %sound.play()

call this function from your coin.gd file

replace pickup_sound.play()

in this

sound_handler.playPickUpSound(global_position)
1 Like

The problem is not that the sound cannot play because the coin gets deleted, its that the sound component is not being recognized as a child of coin at all

Where do you instantiate this scene? Or is it part of your level-scene already?

Yes, as you can see in the image it is a child of coin, and i use it in my game class, apart from the sound it works fine

do you instantiate the scene in code?

I am very new to Godot, but i just dragged them from the list in the bottom left into my game, im not sure if that counts as an instantiation

can you show the scene where you dragged it into?


and in “coins” there are about 12 coins in total

okay first of all you should make the “coins”-node a node2d. You should probably also do this with the “Platforms”-node. Can you show the GameManager-code?


you are right, i changed it to a node2d, i tried it again and i get the same error codes as before. also here is the code of game_manager

hmm thats odd. You only have one “Coin”-scene, right? You can try to drag the audiostreamplayer2d-node inside your coin-script whilst pressing “alt”.

Also can you go into your file-system outside of godot and open the “coin.tscn” in any texteditor and send the content here

here is the entire coin.tscn content, i dont see anything wrong with it, also i did drag audiostreamplayer from the list on the left :[gd_scene load_steps=18 format=3 uid=“uid://d4as2t2cxc2wv”]

[ext_resource type=“Texture2D” uid=“uid://dmdp24tty57m2” path=“res://brackeys_platformer_assets/sprites/coin.png” id=“1_7g6xl”]
[ext_resource type=“Script” path=“res://scripts/coin.gd” id=“1_ur4l7”]
[ext_resource type=“AudioStream” uid=“uid://m4efeg5d7rg6” path=“res://brackeys_platformer_assets/sounds/coin.wav” id=“3_wyucs”]

[sub_resource type=“AtlasTexture” id=“AtlasTexture_vtc2y”]
atlas = ExtResource(“1_7g6xl”)
region = Rect2(0, 0, 16, 16)

[sub_resource type=“AtlasTexture” id=“AtlasTexture_lgdkw”]
atlas = ExtResource(“1_7g6xl”)
region = Rect2(16, 0, 16, 16)

[sub_resource type=“AtlasTexture” id=“AtlasTexture_os31h”]
atlas = ExtResource(“1_7g6xl”)
region = Rect2(48, 0, 16, 16)

[sub_resource type=“AtlasTexture” id=“AtlasTexture_qb1b1”]
atlas = ExtResource(“1_7g6xl”)
region = Rect2(64, 0, 16, 16)

[sub_resource type=“AtlasTexture” id=“AtlasTexture_pjni6”]
atlas = ExtResource(“1_7g6xl”)
region = Rect2(80, 0, 16, 16)

[sub_resource type=“AtlasTexture” id=“AtlasTexture_oqcw4”]
atlas = ExtResource(“1_7g6xl”)
region = Rect2(32, 0, 16, 16)

[sub_resource type=“AtlasTexture” id=“AtlasTexture_3rguk”]
atlas = ExtResource(“1_7g6xl”)
region = Rect2(96, 0, 16, 16)

[sub_resource type=“AtlasTexture” id=“AtlasTexture_jmfqx”]
atlas = ExtResource(“1_7g6xl”)
region = Rect2(112, 0, 16, 16)

[sub_resource type=“AtlasTexture” id=“AtlasTexture_etlph”]
atlas = ExtResource(“1_7g6xl”)
region = Rect2(128, 0, 16, 16)

[sub_resource type=“AtlasTexture” id=“AtlasTexture_f5b7h”]
atlas = ExtResource(“1_7g6xl”)
region = Rect2(144, 0, 16, 16)

[sub_resource type=“AtlasTexture” id=“AtlasTexture_l3qbv”]
atlas = ExtResource(“1_7g6xl”)
region = Rect2(160, 0, 16, 16)

[sub_resource type=“AtlasTexture” id=“AtlasTexture_3gl0s”]
atlas = ExtResource(“1_7g6xl”)
region = Rect2(176, 0, 16, 16)

[sub_resource type=“SpriteFrames” id=“SpriteFrames_kj3yy”]
animations = [{
“frames”: [{
“duration”: 1.0,
“texture”: SubResource(“AtlasTexture_vtc2y”)
}, {
“duration”: 1.0,
“texture”: SubResource(“AtlasTexture_lgdkw”)
}, {
“duration”: 1.0,
“texture”: SubResource(“AtlasTexture_os31h”)
}, {
“duration”: 1.0,
“texture”: SubResource(“AtlasTexture_qb1b1”)
}, {
“duration”: 1.0,
“texture”: SubResource(“AtlasTexture_pjni6”)
}, {
“duration”: 1.0,
“texture”: SubResource(“AtlasTexture_oqcw4”)
}, {
“duration”: 1.0,
“texture”: SubResource(“AtlasTexture_3rguk”)
}, {
“duration”: 1.0,
“texture”: SubResource(“AtlasTexture_jmfqx”)
}, {
“duration”: 1.0,
“texture”: SubResource(“AtlasTexture_etlph”)
}, {
“duration”: 1.0,
“texture”: SubResource(“AtlasTexture_f5b7h”)
}, {
“duration”: 1.0,
“texture”: SubResource(“AtlasTexture_l3qbv”)
}, {
“duration”: 1.0,
“texture”: SubResource(“AtlasTexture_3gl0s”)
}],
“loop”: true,
“name”: &“default”,
“speed”: 10.0
}]

[sub_resource type=“CircleShape2D” id=“CircleShape2D_jebtx”]
radius = 5.0

[node name=“Coin” type=“Area2D”]
collision_mask = 2
script = ExtResource(“1_ur4l7”)

[node name=“AnimatedSprite2D” type=“AnimatedSprite2D” parent=“.”]
sprite_frames = SubResource(“SpriteFrames_kj3yy”)
autoplay = “default”

[node name=“CollisionShape2D” type=“CollisionShape2D” parent=“.”]
shape = SubResource(“CircleShape2D_jebtx”)

[node name=“pickupSound” type=“AudioStreamPlayer2D” parent=“.”]
stream = ExtResource(“3_wyucs”)

[connection signal=“body_entered” from=“.” to=“.” method=“_on_body_entered”]

Okay this is getting really weird.
Instead of using “@onready” can you use “@export” and print out the children in the ready-method:

func _ready():
    print(get_children())


I did it and it printed out the same thing as before, one for each coin loaded in the level right at the start, without the pickupsound.

and the exported variable still throws an error when you assign it to “pickupSound” in the inspector?

Also can you name every scene you have or send a picture of the file-system