关于获取子节点后对其进行操作的问题

let temp = cc.instantiate(this.cardPrefabItem).getComponent(‘Card’);//用prefab创建一个新的card
this.node.addChild(temp.node, 0, 1);//这里用temp,不用temp.node是错的,但是就引发了下边引用的问题
temp.init(1, 1, 0);
temp.highlight.node.active = true;//这里可以将highlight显示
但是如果在其他地方获取这个card:
let test = this.node.getChildByTag(1);
test.highlight.node.active = true;//这样就是错的,我知道返回的test是temp.node,并不是原来的temp,那么如何获得这个temp呢?或是如何把它的highlight设置为true呢?

test是你temp这个Card组件所在的节点

从你这里可以知道,highlight是temp组件引用的一个组件(或者节点)

你所说的temp是test节点(node)下的一个组件,组件的获取方式是 node.getComponent(组件名称或者组件类型),你这里就是test.getComponent(“Card”)

最后就是:test.getComponent(“Card”).highlight.node.active = true

以上是个人看法,仅供参考,如有错误,概不负责:joy:

1赞

谢谢!解决问题了!上次提问也是你帮忙解决的,非常感谢!

:grin:不客气哦,大家互相帮忙