Which node is your script attached to? GetChildByName only looks for direct children, if your script is attached to lets say “content” node, you will have to do
var childNode = this.node.getChildByName("Parent").getChildByName("Child");
to reach “Child”
Also double check for typos. When something essential suddenly stops working it’s 100% guaranteed you’ve just made a typo. A "Child " (with a spacebar) node or something
for(var i = 0; i < this.node.children.length; i++) { // iterate through children by index
var childById = this.node.children[i];
var name = childById.name; // get child node's name
console.log(name);
var childByName = this.node.getChildByName(name); // get child by name
console.log(childByName == childById); // compare them to make sure getChildByName works properly
}
If it outputs true, then you’re using wrong name, if it doesn’t well… Your cocos broke in some mysterious way then
I assume you added the child node through Cocos Creator and the node tree looks like this:
ThisNode
|- Child
This node has the label component so you can access it like
var label = this.node.getChildByName("Child").getComponent(cc.Label);
If it does not work, please check the debugger and look for the child node and its name property
this. node. _children[n]. _name
Or if you add your node in the script component it should look like this:
var node = new cc.Node();
node.addComponent(cc.Label);
this.node.addChild(node, 1, "Child");
var label = this.node.getChildByName("Child").getComponent(cc.Label);
Silly me I haven’t checked your attached image…
So you have the script component on your ‘Parent’ node and cannot retrieve the node named ‘Child’.
Still, check the debugger. Maybe you can find out more this way.
Can you upload your project somewhere and share with us? I’ve been using Cocos Creator for over a month now and have never run into any issues with getChildByName method. Maybe you’re having some compile errors?