Skip to content

Invalid access to property or key 'position' on a base object of type 'Nil' in Godot 4

By Eduardo Orellana · Updated

The variable before the dot is empty. In Godot 4, player.position fails with this error when player holds Nil, which is what a typed variable such as var player: Node2D contains until something assigns it. Assign it, with an @export set in the Inspector or get_node in _ready, and check it before you use it.

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

GDScript compiles player.position to a named property read. At run time the virtual machine asks the value held in player for its position property. When that value has no such property, the function stops and Godot prints this error with the property's name and the type it found.

'Nil' is the name of the empty Variant type: the variable holds nothing at all. A typed variable of an Object type, such as var player: Node2D, starts out holding Nil and keeps it until a line assigns it. The type annotation says what player may hold; it does not create a node.

The same check names the base differently for the other two ways a reference can be empty: 'null instance' when the variable holds an Object reference that points at nothing, which is what $Player and get_node_or_null give you when the node is missing, and 'previously freed' when the object was freed. Each has its own page, linked below.

The function stops at the failing line. In the run below, the print after it never ran.

More on this: Trying to assign a non-object value to a variable of type '…' in Godot 4

Reproduce it

A node script that declares a typed variable and reads a property from it before anything has assigned it. The scene is one Node with this script.

GDScript
extends Node

var player: Node2D

func _ready() -> void:
	print(player.position)
	print("after")

What Godot 4.7.2 prints

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

SCRIPT ERROR: Invalid access to property or key 'position' on a base object of type 'Nil'.
   at: _ready (res://answers/invalid-access-to-property-or-key-on-nil/repro.gd:6)
   GDScript backtrace (most recent call first):
       [0] _ready (res://answers/invalid-access-to-property-or-key-on-nil/repro.gd:6)

Fix it

Export the variable and assign it in the Inspector: here the scene has a Node2D child named Player, and the exported field points at it. The check before use turns a forgotten assignment into a message that names the node, instead of a crash at the first property read.

More on this: Cannot call method '…' on a null value in Godot 4

GDScript
extends Node

# Assigned in the Inspector: drag the Player node onto this field.
@export var player: Node2D

func _ready() -> void:
	if player == null:
		push_error("player is not assigned on %s" % name)
		return
	print(player.position)
	print("after")

What the fixed script prints

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

(64.0, 32.0)
after

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: Can't add child '…' to '…', already has a parent '…' in Godot 4

Prompt
My Godot 4 project prints this error: "Invalid access to property or key 'position' on a base object of type 'Nil'.". Find where it comes from and fix it. The variable before the dot is empty. In Godot 4, player.position fails with this error when player holds Nil, which is what a typed variable such as var player: Node2D contains until something assigns it. Assign it, with an @export set in the Inspector or get_node in _ready, and check it before you use it. Then run the game headless and check that Godot's output no longer shows it.

It is assigned, but later than you read it

Godot calls _ready on children before their parent. If a parent's _ready assigns a child's variable, the child's own _ready runs first and reads it while it is still Nil. Read it later, in a function the parent calls after assigning it, or have the child find what it needs itself with get_node or an @export.

More on this: Invalid call. Nonexistent function 'instance' in base 'PackedScene' in Godot 4

The @export is empty on this instance

An exported field is set per node in the scene. Assigning it on one instance does not assign it on another instance of the same scene, or on a node you create with instantiate() in code. The check in the fix above names the node, so the log tells you which one.

The message names the property you read

The quoted name is only what followed the dot. On an unassigned var player: Node2D, player.global_position prints Invalid access to property or key 'global_position' on a base object of type 'Nil'. A property of your own fails the same way: item.item_defense on an empty var item prints Invalid access to property or key 'item_defense' on a base object of type 'Nil'. Whether or not the property exists, the empty variable before the dot is the cause.

The same error in earlier Godot versions

Godot 4.0 to 4.2, and Godot 3, printed it as: Invalid get index 'position' (on base: 'Nil'). The cause and the fix are the same.

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 6 October 2026, and the output above is what Godot printed, unedited.

Where the check lives in Godot's source: gdscript_vm.cpp line 1281, the message, gdscript_vm.cpp lines 62 to 72, how the base type is named.

Questions

Why does Godot say Nil and not null?
Nil is the name of the empty Variant type in Godot, and null is its only value. The error names the type it found, so an empty variable reads as 'Nil'.
Why did this error used to say Invalid get index?
Godot 4.3 reworded it. Godot 4.0 to 4.2 and Godot 3 printed Invalid get index 'position' (on base: 'Nil'). for the same check. The cause and the fix did not change.
Does the rest of my function run after this error?
No. GDScript stops the function at the failing line. In the run on this page, the print("after") line never ran.

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

Download Flockbay free