Removing and Adding Node to Other Nodes Under the Same Root Node

Godot Version

Godot 4.5

Question

How would I go about taking a Node off of its parent Node, and attaching it to another one inside GDscript?

I am trying to achieve this with the code below, but I truly have no idea what I’m doing. I want an enemy node to become the child of the player node. (Ignore the awagga)


func _on_camera_body_entered(body: Node3D) -> void:
	if body.is_in_group("GHOST"):
		var PARENT = body.get_parent()
		PARENT.remove_child(body)
		add_child(body)
		print("AWAGGA")

Have you tried using reparent?

Assuming this code is attached to your player, then unless I’m missing something this should work:

func _on_camera_body_entered(body: Node3D) -> void:
	if body.is_in_group("GHOST"):
		body.reparent(self)
1 Like

That was it! Thank you!

1 Like

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