im getting an error but its not consistent when debugging, it occasionally happens error> invalid set index 'stream' ( on base: 'previously freed') with value of type AudioStreamSample

func _on_shotgunshell_body_entered(_body):
	#shellmesh.set_layer_mask(1)
	var n = randi() % (shellSounds.size() - 1) + 1
	#error here>
	myAudio.stream = shellSounds[n]
	myAudio.pitch_scale = random.randf_range(0.8, 1.2)
	
	myAudio.play()

It means that the variable myAudio has been deleted. This may have happened if you deleted it using free() or queue_free(). Or it can happen automatically if that variable has gone out of scope, like if it was a temporary variable and the function or object it was living within is also dead.

ah so its probably the onscreen notifier3d?

extends RigidDynamicBody3D


@export var shellSounds : Array[AudioStreamSample]
@export var myAudioStream_path: NodePath
#@export var shellmesh_path: NodePath
#@onready var shellmesh = get_node(shellmesh_path)
@onready var myAudio = get_node(myAudioStream_path)
var random = RandomNumberGenerator.new()
func _ready():
	add_to_group("ignore")
	random.randomize()





func _on_pistolshell_body_entered(_body):
	#shellmesh.set_layer_mask(1)
	var n = randi() % (shellSounds.size() - 1) + 1
	myAudio.stream = shellSounds[n]
	myAudio.pitch_scale = random.randf_range(0.8, 1.2)
	
	myAudio.play()
	


func _on_visible_on_screen_notifier_3d_screen_exited():
	self.queue_free()


func _on_audio_stream_player_3d_finished():
	myAudio.queue_free()

how do i prevent this?

can i check if audio is null? or something , before assigning the stream?

Change the first line to this:

func _on_pistolshell_body_entered(_body):
    if not is_instance_valid(myAudio):
        return
6 months later