Skip to content

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.

Download for macOSDownload for WindowsDownload for Linux

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.

More on this: Invalid operands 'Nil' and 'int' in operator '+' in Godot 4

GDScript
extends Node

func _ready() -> void:
	var picked = [1]
	var node = picked as Node
	print("after")

What Godot 4.7.2 prints

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

More on this: How to parse JSON in Godot 4: JSON.parse_string()

GDScript
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

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

not a node: Array
true

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.

More on this: Invalid assignment of property or key '…' with value of type '…' on a base object of type 'String' in Godot 4

Prompt
My Godot 4 project prints this error: "Invalid cast: can't convert a non-object value to an object type.". Find where it comes from and fix it. 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. Then run the game headless and check that Godot's output no longer shows it.

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

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.

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

Download Flockbay free