英文:
Why is the duplicate of a node's random child null? is there any way to fix or work around it?
问题
So I took a random node from a parent and duplicated that node. For some reason, the Node3D is a null instance, so I can't really do anything with it.
This is my code:
func _ready():
MoveNode
func RandNode(P):
P.get_child(randi() % P.get_child_count()).duplicate()
But, when I try to move it:
func MoveNode()
var NewNode = RandNode($ParentNodePath)
NewNode.global_translate = $OtherNodePath.global_translate
It gives me an error saying that NewNode is null.
英文:
So I took a random node from a parent and duplicated that node. For some reason, the Node3D is a null instance, so I can't really do anything with it.
This is my code:
func _ready():
MoveNode
func RandNode(P):
P.get_child(randi() % P.get_child_count()).duplicate()
But, when I try to move it:
func MoveNode()
var NewNode = RandNode($ParentNodePath)
NewNode.global_translate = $OtherNodePath.global_translate
It gives me an error saying that NewNode is null.
答案1
得分: 0
你复制了节点,但忘了返回它:
英文:
You duplicated the node, but forgot to return it:
func RandNode(P):
return P.get_child(randi() % P.get_child_count()).duplicate()
^^^^^^
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论