节点的随机子节点为什么是空的?有没有办法修复或解决这个问题?

huangapple go评论52阅读模式
英文:

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()
    ^^^^^^

huangapple
  • 本文由 发表于 2023年5月22日 15:03:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/76303707.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定