Find Child not finding child I can see in get_children()

Godot Version

v4.2.2.stable.official [15073afe3]

Question

I am trying to find a child of a node. I am using find_child(name) but it is returning null.

If I run print (button_parent.get_children()) I get

[Expertise-Apprentice:<Button#36138124852>, Description:<Label#36188456503>, Dropdown:<OptionButton#36222010937>, Equipment:<Label#37044094553>, Coin:<Label#37077648986>]

But when I immediately run print (button_parent.find_child("Dropdown")) I get

<Object#null>

I can see a child in the list with the name Dropdown. But when I try and find it I get nothing.

Am I missing a step?

That’s odd…
I tested it and it works fine for me. I put together a control, a button and an option button.
image

Then a ready function:
image

And it works fine…
image

I mean, those buttons seem to be fixed. Wouldn’t it be better to have a reference already set up instead of searching by name every time you need them?

Like this:

@onready var dropdown = $Control/Dropdown

I would but the children are generated by code, not manually added to the scene in the editor. That may be the source of the problem.

Oh… in that case maybe the third parameter is the reason:

If you dynamically create them, I’m not sure if it’s owned or not. Try calling:

print (button_parent.find_child("Dropdown", true, false))

Alternatively, you can use get_node instead of find_child, it’s more widely used.

Node find_child ( String pattern, bool recursive=true, bool owned=true ) const
Finds the first descendant of this node whose name matches

Try this:

for i in button_parent.get_children():
	print(i.name)

Yes, that is the reason.
If you dynamically create them, they’re not “owned” by the parent node.
Here’s an example, I created a new button and added it to the control.
image

get_children gives me the new button.
find_child doesn’t return the button
find_child with third parameter=false returns the button
get_node returns the button

I’d recommend you use get_node instead of find_child.

Alternatively, if you REALLY want to use find_child, you need to also set the parent as owner (in my case, the control node) for it to work.
See the console, now all calls return the button.