Node 3D instance cannot be removed

Godot Version

4.2.2

Question

Hello, I need help removing the node 3d instance which is the top bread object. When the game restarts, the bread object is still there. However, when I tried using it as a rigid body, it does get removed but I want the sandwich to have a compressed effect. Is there another way to do this? Pls help I appreciate all help :sandwich:

ezgif-7-cdd5112c0c

Screenshot 2024-08-03 at 11.15.12 PM

#top bread object
@onready var bread_scene: PackedScene = preload("res://objects/bread_top.tscn")
var bread_instance: Node3D = null
var bread_target_position: Vector3
var bread_spawned = false
#checks if the ingredient is correct or wrong
func _on_ingredient_landed(ingredient):
	if fall_index < hbox_container.generated_sequence.size():
		if ingredient.ingredient_type == hbox_container.generated_sequence[fall_index]:
			print("Correct")
		else:
			print("Wrong")
			_lose_heart()
			ingredient.queue_free()

	#Player wins so the bread should fall
	if player_sequence.size() == hbox_container.generated_sequence.size():
		if player_sequence == hbox_container.generated_sequence:
			print("Player won!")
			if bread_spawned:
				bread_instance.queue_free()
			bread_instance = bread_scene.instantiate()
			bread_instance.global_transform.origin = Vector3(0, 10, 0)  
			bread_target_position = Vector3(0, 1, 0) 
			add_child(bread_instance)
			bread_spawned = true
			restart_timer.start()
#makes bread object fall
func _process(delta):
	if bread_instance:
		var direction = bread_target_position - bread_instance.global_transform.origin
		var distance = direction.length()
		var move_step = direction.normalized() * 5 * delta  #bread speed
		if distance > move_step.length():
			bread_instance.global_transform.origin += move_step
		else:
			bread_instance.global_transform.origin = bread_target_position
			bread_instance = null
#restart button. The bread should be removed here
func _on_button_pressed():
	# Removes the bread instance if it exists
	if bread_instance and is_instance_valid(bread_instance):
		bread_instance.queue_free()
		bread_instance = null

	# Reset bread postion
	bread_target_position = Vector3.ZERO
	bread_spawned = false
#makes bread object fall
func _process(delta):
    if bread_instance:
	...
		if distance > move_step.length():
			bread_instance.global_transform.origin += move_step
		else:
			bread_instance.global_transform.origin = bread_target_position
			bread_instance = null

If distance is not greater then step you set the bread_instance to null. I don’t think that is correct. After you do that the process stops, but you can no longer free it on the reset button press.

You should just remove the = null line. Everything else looks fine I think.

1 Like

thank youu pennyloafers!! The bread works! :bread: :sparkling_heart:

ezgif-7-ab56870147

1 Like

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