Problems with get_child().get_child()

Godot Version

4.2.1

Question

I’m making something in 2D and I have a problem with get_child().get_child(), I’m not even sure if this is a thing that’s possible, but I couldn’t find anything about it on the internet

I have a Node2D with a bunch of scene instances (Node2Ds) with two sprites, named “Red” and “White”. In the first Node2D I have this script:

extends Node2D

func _process(delta):
	var count = get_child_count()
	for i in count:
		get_child(i).get_child(0).position = Vector2.ZERO
		get_child(i).get_child(1).position = Vector2.ZERO
		
		if get_node("/root/Global").visualEffectsOn == false:
			get_child(i).queue_free()

With this, get_child(i) works, but if I try to get a child of that, it doesn’t. How do I fix this?

Thank you

chained get_child() calls in this example work. please provide some information about the problem. are you getting a warning or error message?

1 Like

The error I’m getting is “Invalid get index ‘position’ (on base: ‘null instance’).” Implying that it indeed can’t get the node. Maybe the problem is that I’m trying to get a child from a scene?

correct. in my example if i tried to do: get_child(0).get_child(2).get_child(0).get_child(0).name i would get the same error because there is no get_child(0).get_child(2). use the print command to verify data. below is an example, verify the child node count in get_child(i).

print(get_child(i).get_child_count())
#get_child(i).get_child(0).position = Vector2.ZERO
1 Like

Wow that’s really weird, it just prints 2 now so I guess it can see the child nodes

1 Like

I rewrote the code in the exact same way, and it seems to work now, I guess there was a typo or something. Thanks for helping me