0 votes

I am trying to change the parent of my current node.

get_parent().remove_child(self)
get_parent().get_parent().add_child(self)
asked Jun 15, 2019 in Engine by usurun (322 points)

1 Answer

0 votes
Best answer

When you unparent a node, the return value of get_parent() will be null. So you cannot use get_parent() when it has no parent.

var new_parent = get_node("/root/AnotherParent")
get_parent().remove_child(self)
new_parent.add_child(self)



Reference the parent before you unparent:

var new_parent = get_parent().get_parent()
get_parent().remove_child(self)
new_parent.add_child(self)
answered Jun 15, 2019 by sparkart (327 points)
selected Jun 15, 2019 by usurun

somehow I get 'addchild' in base 'null instance' after removechild
func _process(delta): if target and get_parent() != get_node(target): var new_parent = get_node(target) get_parent().remove_child(self) new_parent.add_child(self)

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.