I can't play an audio

Godot Version

4

Question

I get this "Cannot call method ‘play’ on a null value.” when I touch a object this is the script

ass_name Item extends Area2D
@exportexport var reproductor: AudioStreamPla@exporter2D
@export var texture: CompressedTexture2D:
set(value):
texture = value
$Sprite2D.texture = value
enum ItemType{Iphone,Balon,Luces,Pizza,Mando}
@export var type:ItemType

func collect_item():

reproductor.play()
queue_free()

func get_texture() → CompressedTexture2D:

return $Sprite2D.texture

All of this is in gdscript.

Please use preformatted text with three backticks ``` for code snippets, otherwise it’s very hard to read.

This error means the object on which you’re calling the play() method is null at the time of calling. In your code, I can see only this part where play() method is mentioned:

That means, reproductor is most likely not assigned in the Inspector. Can you check if you assigned something to the reproductor variable?

Sorry I am new at this forum. I assigned the variable and the mistake still there.

Before the faulty line, add this print statement, what does it print to the Output console?

print(reproductor)
reproductor.play()

<null>

Well then if it’s printed as null, then you might want to recheck your assignment, because it means it’s not assigned.
You can also go into the Remote scene tree after starting the game and see how your objects are assigned

reproductor is a member variable of your object instance.
You play the sound but immediately queue your object up for deletion.
My guess is that your object gets deleted (and takes reproductor down with it) before you hear a sound.
Comment out queue_free() and see if your sound plays.

reproductor.play()
#queue_free()

Perhaps adding a timer could solve the problem.

The problement is not the queue_free() is that is not assigned. Thank you for your concern.