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

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.

Source: https://flockbay.com/godot/unable-to-iterate-on-object-of-type-nil/ · Updated 2026-10-30

## 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
```

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

```
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](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.

Download Flockbay free: Flockbay's agent reads Godot's debugger output and fixes errors like this one in your project, on the official Godot. https://flockbay.com/download/

Related: [Unable to obtain iterator object of type 'Object' (but was obtained on first iteration?) in Godot 4](/godot/unable-to-obtain-iterator-object-but-was-obtained-on-first-iteration/), [Invalid operands 'Nil' and 'int' in operator '+' in Godot 4](/godot/invalid-operands-nil-and-int/), [Invalid call. Nonexistent function '…' in base 'Nil' in Godot 4](/godot/nonexistent-function-in-base-nil/), [Invalid access to property or key 'position' on a base object of type 'Nil' in Godot 4](/godot/invalid-access-to-property-or-key-on-nil/), [Every verified Godot 4 answer](/godot/), [How do I see Godot’s output and errors in Flockbay?](/docs/how-do-i-see-godots-output-in-flockbay/)
