Error after reparent node

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By dancaer69

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.

:bust_in_silhouette: Reply From: dancaer69

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.