Unable to iterate on object of type 'Nil' in Godot 4

By Eduardo Orellana · Updated

The for loop's container is null. for item in items with items never assigned stops with Unable to iterate on object of type 'Nil'. Give the variable an array when you declare it, var items := [], or check it before the loop.

AppImage, x86_64. Ubuntu 22.04, Debian 12, Fedora 39 or newer.

Intel or AMD Chromebook, with Linux turned on.

Download for macOSDownload for Windows

What Godot is checking

A for loop over an untyped value asks it to start iterating. Arrays, Dictionaries, Strings, ints, floats, vectors and objects with _iter_init can; null cannot, and the VM names its type, Nil.

The same message names any other value that cannot be iterated: a bool gives Unable to iterate on object of type 'bool'.

Reproduce it

An items variable declared without a value, then looped over.

extends Node

var items

func _ready() -> void:
	for item in items:
		print(item)
	print("after")

What Godot 4.7.2 prints

Godot Engine v4.7.2.stable.mono.official.ed1daf0bf - https://godotengine.org

SCRIPT ERROR: Unable to iterate on object of type 'Nil'.
   at: _ready (res://answers/unable-to-iterate-on-object-of-type-nil/repro.gd:6)
   GDScript backtrace (most recent call first):
       [0] _ready (res://answers/unable-to-iterate-on-object-of-type-nil/repro.gd:6)

Fix it

Start it as an empty typed array; a loop over an empty array runs zero times.

extends Node

var items: Array[String] = []

func _ready() -> void:
	for item in items:
		print(item)
	items.append("key")
	for item in items:
		print("has ", item)

What the fixed script prints

Godot Engine v4.7.2.stable.mono.official.ed1daf0bf - https://godotengine.org

has key

Verified on Godot 4.7.2

Both scripts on this page were run headless on Godot 4.7.2 (4.7.2.stable.mono.official.ed1daf0bf) on 18 October 2026, and the output above is what Godot printed, unedited.

Where the check lives in Godot's source: [gdscript_vm.cpp line 3043, the message](https://github.com/godotengine/godot/blob/4.7.2-stable/modules/gdscript/gdscript_vm.cpp#L3043).

Questions

Can I loop over a number in Godot 4?
Yes. for i in 3 runs with 0, 1 and 2, the same as range(3).
What prints Trying to iterate on a null value instead?
A loop over a typed variable that holds null, such as a typed Object variable. An untyped null gives the Nil message on this page.