Trying to assign a non-object value to a variable of type '…' in Godot 4
By Eduardo Orellana · Updated
A variable whose type is a script class was given a value that is not an object, such as a String or an int. Godot names the type by its script's file name, so target = picks["target"] with a String in the Dictionary stops with Trying to assign a non-object value to a variable of type 'repro.gd'; store the node or resource itself, not its name, and look the object up first if all you have is a name.
What Godot is checking
When a value is assigned to a variable typed with a GDScript class (a class_name script or a preloaded script constant), the VM checks the value at runtime. Any Variant type other than Object or null fails this check, and the message names the type by the script's file name, from get_path().get_file(), not by its class_name.
null passes the check, and an object passes only if its script is the declared script or extends it. An object whose script is unrelated, or which has no script, fails with a different message: Trying to assign value of type '…' to a variable of type '…'.
The same text is built when a value is cast with as to a script class, so some_string as Enemy fails the same way.
It is a runtime error: the script parsed, because picks["target"] comes from an untyped Dictionary and is a Variant until the line runs. The function stops on the failing line, so the print("after") below it never ran, as the output shows.
Reproduce it
A script typed by a preloaded script constant, Enemy, gets its target from an untyped Dictionary that holds the enemy's name, a String, instead of the enemy itself.
extends Node
const Enemy = preload("res://answers/trying-to-assign-a-non-object-value-to-a-variable-of-type/repro.gd")
var target: Enemy
func _ready() -> void:
var picks = {"target": "Goblin"}
target = picks["target"]
print("after")
What Godot 4.7.2 prints
Godot Engine v4.7.2.stable.mono.official.ed1daf0bf - https://godotengine.org
SCRIPT ERROR: Trying to assign a non-object value to a variable of type 'repro.gd'.
at: _ready (res://answers/trying-to-assign-a-non-object-value-to-a-variable-of-type/repro.gd:9)
GDScript backtrace (most recent call first):
[0] _ready (res://answers/trying-to-assign-a-non-object-value-to-a-variable-of-type/repro.gd:9)Fix it
Store the object, here the node itself, so the value is an instance of the declared script. If you only have a name, look the node up first, for example with get_node(), and assign what it returns.
extends Node
const Enemy = preload("res://answers/trying-to-assign-a-non-object-value-to-a-variable-of-type/fix.gd")
var target: Enemy
func _ready() -> void:
var picks = {"target": self}
target = picks["target"]
print("target is ", target.name)
What the fixed script prints
Godot Engine v4.7.2.stable.mono.official.ed1daf0bf - https://godotengine.org
target is FixVerified 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 1581, the message on assignment](https://github.com/godotengine/godot/blob/4.7.2-stable/modules/gdscript/gdscript_vm.cpp#L1581), [gdscript_vm.cpp line 1569, the typed script assignment check](https://github.com/godotengine/godot/blob/4.7.2-stable/modules/gdscript/gdscript_vm.cpp#L1569), [gdscript_vm.cpp line 1703, the same message on an as cast](https://github.com/godotengine/godot/blob/4.7.2-stable/modules/gdscript/gdscript_vm.cpp#L1703), [preload in the class reference](https://github.com/godotengine/godot/blob/4.7.2-stable/modules/gdscript/doc_classes/@GDScript.xml#L190).
Questions
- Why does the message name a .gd file instead of my class_name?
- The VM builds the type name from the script's path with get_path().get_file(), so a variable typed Enemy is reported by the file that defines Enemy, here repro.gd.
- Can I assign null to a variable typed with a script class?
- Yes. The check lets null through and stops only values whose Variant type is neither Object nor null.
- Did older Godot versions print a different message?
- Godot 4.2 builds the same text: Trying to assign a non-object value to a variable of type '…'.
Related
- Invalid type in static function. Cannot convert argument
- Invalid access to property or key on a base object of type 'Nil'
- Every verified Godot 4 answer
- Invalid assignment of property or key with value of type on a base object of type 'Nil' in Godot 4
- Invalid call to function '…' in base '…'. Expected N argument(s) in Godot 4
- Invalid access to property or key on a base object of type 'null instance' in Godot 4
- Trying to call an async function without "await" in Godot 4
- Invalid access to property or key on a base object of type 'previously freed' in Godot 4
Flockbay's agent reads Godot's debugger output and fixes errors like this one in your project, on the official Godot.
Download Flockbay free