+1 vote

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

get_parent().remove_child(self)
get_parent().get_parent().add_child(self)
in Engine by (329 points)

1 Answer

+1 vote
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)
by (331 points)
selected by

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.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.