For child in node for loop not iterating through all children

Godot Version

4.7.2

Question

I’m trying to figure out why this for loop is not picking up all the children it has in the scene tree. It only goes through the first units(Boar and Varre) and skips Margery. Thank you in advance for any help!

extends Node2D
@onready var tilemap = $TileMapLayer

func getunits():
	for child in tilemap.get_children():
		print("child: " + str(child))
		if str(child).contains("Unit"):
			units.append(child)
			unitpositions[child] = Vector2i(floor(child.position/32))
		if str(child).contains("Enemy"):
			enemies.append(child)
			enemypositions[child] = Vector2i(floor(child.position/32))
			print("enemy positions: " + str(enemypositions))
	print(unitpositions)
	print(enemypositions)

Output:
child: UnitBoar:<AnimatedSprite2D#45348816634>
child: UnitVarre:<AnimatedSprite2D#45365593851>
{ UnitBoar:<AnimatedSprite2D#45348816634>: (20, 4), UnitVarre:<AnimatedSprite2D#45365593851>: (25, 8) }

Check the remote scene tree at runtime to see what actual children are there.

You also have some errors reported by the debugger. You should address those.

This is what it looks like while open in the remote scene tree. The errors were just unused variables like delta and the like. One thing I noticed is the get_children command actually does get all the children when stored into a variable, but iterating through that same variable still misses the rest of the list, so maybe that narrows it down.

Post the entire script.
Who calls getunits()?

The ready() function calls getunits(). Apparently just restarting Godot stopped this weird behavior though, so I’ll mark it as solved. Strange bug.