Collision does not work after removing node and adding it again

Godot Version

4

Question

Hello, I’m trying to make a sort of “puzzle”, and when the player activates a tile, a “door” is removed and if the tile is deactivated that door reappears again. I’m doing so by removing and adding the node that corresponds to the door. The problem is, it works when the scene starts but if I activate and deactivate the key tile, the CollisionShape2D reactivates but does not collide with the player. Here’s the code:

@onready var door: StaticBody2D = $PuzzleDoor
var puzzle_parts: Array = []
var total_parts: int = 0
var completed = false

func _process(delta):
	if total_parts == 0 and !completed:
		$PuzzleDoor/CollisionShape2D.disabled = true
		completed = true
		dooranim.play("disappear")

func _part_activated(status, part):
	if status:
		total_parts -= 1
	else:
		completed = false
		total_parts += 1
		add_child(door)
		$PuzzleDoor/CollisionShape2D.disabled = false
		dooranim.play("reappear")
	pass

As per the documentation for the disabled variable, you should be setting the value with the set_deferred function.

$PuzzleDoor/CollisionShape2D.set_deferred("disabled", false)

That will hopefully properly enable the collision when adding it again.

1 Like

Thank you! That solved the bug

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.