Invalid assignment of property or key with value of type on a base object of type 'Nil' in Godot 4
By Eduardo Orellana · Updated
You are setting a property on an empty variable. In Godot 4, player.position = Vector2(4, 2) 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 the variable first, then set its properties.
What Godot is checking
GDScript compiles player.position = Vector2(4, 2) to a named property write. At run time the virtual machine asks the value held in player to set its position. An empty variable has no properties to set, so the function stops and Godot prints the property, the type of the value you tried to write, and the type it found.
'Nil' is the empty Variant type. A typed variable of an Object type, such as var player: Node2D, holds Nil until a line assigns it: the type annotation does not create a node.
Reading a property from the same empty variable fails with the matching read error, Invalid access to property or key, which has its own page.
Reproduce it
A node script that declares a typed variable and sets a property on it before anything has assigned it. The scene is one Node with this script.
extends Node
var player: Node2D
func _ready() -> void:
player.position = Vector2(4, 2)
print("after")
What Godot 4.7.2 prints
Godot Engine v4.7.2.stable.mono.official.ed1daf0bf - https://godotengine.org
SCRIPT ERROR: Invalid assignment of property or key 'position' with value of type 'Vector2' on a base object of type 'Nil'.
at: _ready (res://answers/invalid-assignment-of-property-or-key-on-nil/repro.gd:6)
GDScript backtrace (most recent call first):
[0] _ready (res://answers/invalid-assignment-of-property-or-key-on-nil/repro.gd:6)Fix it
Assign the variable before writing to it. Here it is exported and set in the Inspector to the scene's Player child; creating the node in code with Node2D.new() and add_child() works the same way.
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
player.position = Vector2(4, 2)
print(player.position)
print("after")
What the fixed script prints
Godot Engine v4.7.2.stable.mono.official.ed1daf0bf - https://godotengine.org
(4.0, 2.0)
afterThe same error in earlier Godot versions
Godot 4.0 to 4.2, and Godot 3, printed it as: Invalid set index 'position' (on base: 'Nil') with value of type 'Vector2'. 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 1235, the message](https://github.com/godotengine/godot/blob/4.7.2-stable/modules/gdscript/gdscript_vm.cpp#L1235), [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 did this error used to say Invalid set index?
- Godot 4.3 reworded it. Godot 4.0 to 4.2 and Godot 3 printed Invalid set index 'position' (on base: 'Nil') with value of type 'Vector2'. for the same check.
- 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
- Invalid access to property or key on a base object of type 'Nil'
- Invalid access to property or key on a base object of type 'null instance'
- Invalid access to property or key on a base object of type 'previously freed'
- 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 type in static function '…' in type '…'. Cannot convert argument in Godot 4
- Invalid call. Nonexistent function 'instance' in base 'PackedScene' 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