Skip to content

Invalid assignment of property or key 'text' with value of type 'String' 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, label.text = "Score" or player.position = Vector2(4, 2) fails with this error when the variable 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.

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

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

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.

GDScript
extends Node

var player: Node2D

func _ready() -> void:
	player.position = Vector2(4, 2)
	print("after")

What Godot 4.7.2 prints

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

More on this: Cannot assign a new value to a constant 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
	player.position = Vector2(4, 2)
	print(player.position)
	print("after")

What the fixed script prints

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

(4.0, 2.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.

Prompt
My Godot 4 project prints this error: "Invalid assignment of property or key 'position' with value of type 'Vector2' on a base object of type 'Nil'.". Find where it comes from and fix it. You are setting a property on an empty variable. In Godot 4, label.text = "Score" or player.position = Vector2(4, 2) fails with this error when the variable 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. Then run the game headless and check that Godot's output no longer shows it.

The message names the property and the value you set

The quoted parts are only what the failing line wrote. On a var label: Label that nothing has assigned, label.text = "Score" prints Invalid assignment of property or key 'text' with value of type 'String' on a base object of type 'Nil'. On an unassigned var sprite: Sprite2D, setting texture to a CompressedTexture2D, the class an imported image loads as, prints Invalid assignment of property or key 'texture' with value of type 'CompressedTexture2D' on a base object of type 'Nil'. Whatever the property, the empty variable before the dot is the cause, and the fix above applies.

More on this: Cannot find member "…" in base "…" in Godot 4

The 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, gdscript_vm.cpp lines 62 to 72, how the base type is named.

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.
What if the message ends in 'null instance' instead of 'Nil'?
Then the variable holds a reference from a node lookup that found no node, such as $Label in a scene with no child named Label. label.text = "Score" on it prints Invalid assignment of property or key 'text' with value of type 'String' on a base object of type 'null instance'. Fix the node path; the 'null instance' page linked below covers that case.
What if 'Nil' is the type of the value, not the base?
Then the object before the dot is fine and the value you are writing is empty. spawn.z = data.get("z") on a Vector3, with no "z" key in the Dictionary, prints Invalid assignment of property or key 'z' with value of type 'Nil' on a base object of type 'Vector3'. Check the value before you assign it, or give get() a default: data.get("z", 0.0).

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

Download Flockbay free