# Invalid cast: can't convert a non-object value to an object type in Godot 4

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.

Source: https://flockbay.com/godot/invalid-cast-cant-convert-a-non-object-value-to-an-object-type/ · Updated 2026-10-30

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

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

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

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 cast: could not convert value to '…' in Godot 4](/godot/invalid-cast-could-not-convert-value/), [Trying to assign a non-object value to a variable of type '…' in Godot 4](/godot/trying-to-assign-a-non-object-value-to-a-variable-of-type/), [Trying to cast a freed object in Godot 4](/godot/trying-to-cast-a-freed-object/), [Trying to assign value of type 'String' to a variable of type 'int' in Godot 4](/godot/trying-to-assign-value-of-type-string-to-int/), [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/)
