Invalid access to property or key 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.
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.
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.
extends Node
var player: Node2D
func _ready() -> void:
print(player.position)
print("after")
What Godot 4.7.2 prints
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.
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
Godot Engine v4.7.2.stable.mono.official.ed1daf0bf - https://godotengine.org
(64.0, 32.0)
afterIt 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.
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 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](https://github.com/godotengine/godot/blob/4.7.2-stable/modules/gdscript/gdscript_vm.cpp#L1281), [gdscript_vm.cpp lines 62 to 72, how the base type is named](https://github.com/godotengine/godot/blob/4.7.2-stable/modules/gdscript/gdscript_vm.cpp#L62-L72).
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.
Related
- The same error on a base object of type 'null instance'
- The same error on a base object of type 'previously freed'
- Invalid assignment of property or key on a base object of type 'Nil'
- Every verified Godot 4 answer
- Trying to assign a non-object value to a variable of type '…' in Godot 4
- Invalid call to function '…' in base '…'. Expected N argument(s) in Godot 4
- Invalid call. Nonexistent function 'instance' in base 'PackedScene' in Godot 4
- Invalid type in static function '…' in type '…'. Cannot convert argument in Godot 4
- The "onready" keyword was removed in Godot 4: use @onready
Flockbay's agent reads Godot's debugger output and fixes errors like this one in your project, on the official Godot.
Download Flockbay free