0 votes

I'm trying to use reparent but I get an error after reparenting.

func on_redBalls_pressed(viewport, event, shape_idx):
    if event is InputEventMouseButton:
        if event.button_index == BUTTON_LEFT and not event.is_echo() and event.is_pressed():
            print("pressed")
            temp_parenting($RedBalls, $RedZone)


func temp_parenting(child, new_parent):
    for i in $RedZone/RedPositions.get_children().size():
        if child.global_position == $RedZone/RedPositions.get_child(i).global_position:
            var old_parent = child.get_parent()
            old_parent.remove_child(child)
            child.position = $RedZone/RedPositions.get_child(i).position
            new_parent.add_child(child)
            new_parent.set_owner(child)
            break

When the method called first time works, but then I get error when the function called the second time because "child" is null.

related to an answer for: Reparent node at runtime
in Engine by (219 points)

1 Answer

0 votes

I' ll answer myself because I found what was wrong with above. I'm running on mouse press, so after the reparenting is done, the $RedBall node has a new parent so it doesn't exist any more in the old parent. So the second time gives the null error.
I changed the code and I first checking if the node exists, then run the function and no works fine.

by (219 points)
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.