How to make an object disappear completely

Godot Version

4

Question

so in my game when you hit an item it disappears and is registered, but it only becomes invisible, not gone, so if you go over the same area twice, it gets registered twice. what could i add to the code to make it completely disappear?


func _on_body_entered(body: Node2D) -> void:
	if body.is_in_group("playerreal"):
		World.has_bread = true
		self.visible = false
		World.register_bread()

Would a simple queue_free() work for you?

As already said, you can use queue_free() instead of visible = false to destroy the object. Note that once a node is freed, there’s no way you can access it anymore later on.

So, I don’t know exactly what you are looking for, but in case you want to disable it without destruction (the typical case is if you need to re-activate it later on), visible = false is not enough as although the object is invisible, it’s still considered active (visible =/= active).
On CollisionShape2D nodes, there’s a disabled property you can use, that’s false by default, and that you can set to true to disable any collision detection on that object. That should be enough.

Hope that helps!

1 Like

If you want to keep the object around but have it “not there” for gameplay purposes, you could always set its position to somewhere far off in space.

Also, it’s worth noting that setting visible to false doesn’t stop things from having their update functions called. is_visible_in_tree() can help there.

1 Like

Hi, A simple queue_free() can work!

1 Like