var root_node
var child_node
func _ready() -> void:
root_node = get_node("../")
child_node = get_node("child")
print(root_node)
test()
func test():
var load_scene = load("res://suika_crane/_test/rigid_body_2d.tscn")
var ins = load_scene.instantiate()
#this is not work
root_node.add_child(ins)
#this is work
#child_node.add_child(ins)
func _input(event: InputEvent) -> void:
if Input.is_action_just_pressed("ui_accept"):
test()
why add_child not work in this code?
i tested some,and i nortice a few things.
1:add_child not work in ready function.
2:add_child work in input function.
3:add_child work with child node Even when used in ready.
I was able to get the top node without any problems.
Please lend me some help.
Unless I am mistaken, I think root_node might be null when you are trying to add the child node. It might have to do with how you are getting the node.
I think that it should be without the forward slash, so root_node = get_node("..")
If that doesn’t work, try using root_node = get_parent() instead?
var root_node
var root_node2
var child_node
func _ready() -> void:
root_node = get_node("..")
root_node2 = get_parent()
print(root_node,root_node2)
child_node = get_node("child")
test()
func test():
var load_scene = load("res://suika_crane/_test/rigid_body_2d.tscn")
var ins = load_scene.instantiate()
#this is not work
root_node.add_child(ins)
root_node2.add_child(ins)
#this is work
child_node.add_child(ins)
add child not work with get_node("..") and get_parent()
,but i can get top node with both.
Put pass after the last line of function test and set a breakpoint on it and run scene.
Using the debugger at breakpoint to check for any errors in the tab and also click on the scene structure Remote tab to see what your tree looks like while debugger is stopped at breakpoint.
Oh should see the nodes someplace or at least some errors.