Name from list is not deleted when object is deleted

Godot Version 4

Hello, I’m making a simple game in 3D for a project and I’m having issues making a name erase from a list that is shown on screen. What’s supposed to happen is when one of the objects in the list is clicked, it is deleted and the name in the list is removed. However, the clicking and deleting are working but erasing the name isn’t. Here is the script.


extends RayCast3D

@onready var prompt = $Prompt
@onready var objects_left = ["Red ring", "Blue box", "Green sphere"]

func _physics_process(_delta):
prompt.text = "Objects left: " + ", ".join(objects_left)

if is_colliding():
var collider = get_collider()
if objects_left.has(collider.name):  
if Input.is_action_just_pressed("click"):  
collider.queue_free()
objects_left.erase(collider)

I’m still a newbie to Godot so I may not know a simple fix, thanks!

Change the order and try again
objects_left.erase(collider)
collider.queue_free()

What @hallucination said is important but also another detail, you look for the collision.name when check if the array has the item, but try to erase the collision object instead. You should use objects_left.erase(collider.name)