Invalid cast: can't convert a non-object value to an object type in Godot 4
By Eduardo Orellana · Updated
The value on the left of as is not an Object, so it cannot be cast to a class such as Node. [1] as Node stops with Invalid cast: can't convert a non-object value to an object type. Check what the value is with typeof() or is before casting; as only converts objects to object types.
AppImage, x86_64. Ubuntu 22.04, Debian 12, Fedora 39 or newer.
Intel or AMD Chromebook, with Linux turned on.
What Godot is checking
as to a class type is a checked downcast for objects: it returns the object when it is that class, and null when it is a different class. A built-in value such as an Array, an int or a String is not an object at all, and the VM stops instead of returning null.
Casting between built-in types is a different instruction. An untyped variable holding "hello", cast as int, converts and gives 0 with no error; a String literal cast as int is refused at parse time with Cannot cast a value of type "String" as "int".
More on this: Trying to assign a non-object value to a variable of type '…' in Godot 4
Reproduce it
An array in an untyped variable, cast to Node.
extends Node
func _ready() -> void:
var picked = [1]
var node = picked as Node
print("after")
What Godot 4.7.2 prints
Godot Engine v4.7.2.stable.mono.official.ed1daf0bf - https://godotengine.org
SCRIPT ERROR: Invalid cast: can't convert a non-object value to an object type.
at: _ready (res://answers/invalid-cast-cant-convert-a-non-object-value-to-an-object-type/repro.gd:5)
GDScript backtrace (most recent call first):
[0] _ready (res://answers/invalid-cast-cant-convert-a-non-object-value-to-an-object-type/repro.gd:5)Fix it
Check that the value is a Node first; as then returns it or null.
extends Node
func _ready() -> void:
var picked = [1]
if picked is Node:
print(picked as Node)
else:
print("not a node: ", type_string(typeof(picked)))
var other = Node.new()
print((other as Node2D) == null)
other.free()
What the fixed script prints
Godot Engine v4.7.2.stable.mono.official.ed1daf0bf - https://godotengine.org
not a node: Array
trueVerified 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 18 October 2026, and the output above is what Godot printed, unedited.
Where the check lives in Godot's source: [gdscript_vm.cpp line 1671, the message](https://github.com/godotengine/godot/blob/4.7.2-stable/modules/gdscript/gdscript_vm.cpp#L1671).
Questions
- What does as return when the object is the wrong class?
- null, with no error. as Node2D on a plain Node returns null, as the fix printed. Only a non-object value is an error.
- How do I convert a String to a number?
- Use to_int() or to_float(). They read the digits and give 0 for text that is not a number, so check is_valid_int() when that matters.
Related
- Invalid cast: could not convert value to 'int'
- Trying to cast a freed object
- Every verified Godot 4 answer
- Trying to assign value of type 'String' to a variable of type 'int' in Godot 4
- Trying to return a value of type "…" from a function whose return type is "…" in Godot 4
- Cannot infer the type of "…" variable because the value is "null" in Godot 4
- How to parse JSON in Godot 4: JSON.parse_string()
- Trying to assign a non-object value to a variable of type '…' 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