Skip to content

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 WindowsDownload for Linux

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'.

More on this: Invalid assignment on read-only value (on base: 'Array') in Godot 4

Reproduce it

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

GDScript
extends Node

var items

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

What Godot 4.7.2 prints

Output
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.

More on this: Out of bounds get index '…' (on base: 'Array') in Godot 4

GDScript
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

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

has key

Run this fix with your agent

Flockbay is the Godot editor with an AI agent built in. Paste this into Flockbay's chat, or into Claude Code or Codex connected to Flockbay over MCP: the agent finds the cause in your project, applies the fix, runs the game headless and reads Godot's output to check it.

More on this: Cannot infer the type of "…" variable because the value is "null" in Godot 4

Prompt
My Godot 4 project prints this error: "Unable to iterate on object of type 'Nil'.". Find where it comes from and fix it. 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. Then run the game headless and check that Godot's output no longer shows it.

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.

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.

Flockbay's agent reads Godot's debugger output and fixes errors like this one in your project, on the official Godot.

Download Flockbay free