# Invalid assignment of property or key 'text' with value of type 'String' on a base object of type 'Nil' in Godot 4

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.

Source: https://flockbay.com/godot/invalid-assignment-of-property-or-key-on-nil/ · Updated 2026-10-30

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

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

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

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

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: [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/), [Invalid access to property or key 'position' on a base object of type 'null instance' in Godot 4](/godot/invalid-access-to-property-or-key-on-null-instance/), [Invalid access to property or key on a base object of type 'previously freed' in Godot 4](/godot/invalid-access-to-property-or-key-on-previously-freed/), [Invalid assignment of property or key '…' with value of type '…' on a base object of type 'Node2D' in Godot 4](/godot/invalid-assignment-of-property-or-key-on-node2d/), [Invalid assignment of property or key '…' with value of type '…' on a base object of type 'String' in Godot 4](/godot/invalid-assignment-of-property-or-key-on-string/), [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/)
