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

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?

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