# Trying to cast a freed object in Godot 4

The variable points at an object that was freed, and as cannot cast what no longer exists. enemy as Node after enemy.free() stops with Trying to cast a freed object. Check is_instance_valid(enemy) before the cast.

Source: https://flockbay.com/godot/trying-to-cast-a-freed-object/ · Updated 2026-10-30

## What Godot is checking

A cast to a class looks up the object to read its class. Freeing an object leaves variables holding a reference to nothing, and the VM stops with this message rather than returning null.

queue_free() frees at the end of the frame, so a reference kept until the next frame is in the same state.

## Reproduce it

A node freed, then cast with as.

```
extends Node

func _ready() -> void:
	var enemy = Node.new()
	enemy.free()
	var target = enemy as Node
	print("after")

```

## What Godot 4.7.2 prints

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

SCRIPT ERROR: Trying to cast a freed object.
   at: _ready (res://answers/trying-to-cast-a-freed-object/repro.gd:6)
   GDScript backtrace (most recent call first):
       [0] _ready (res://answers/trying-to-cast-a-freed-object/repro.gd:6)
```

## Fix it

Cast only an object that still exists.

```
extends Node

func _ready() -> void:
	var enemy = Node.new()
	enemy.free()
	var target: Node = enemy as Node if is_instance_valid(enemy) else null
	print("target: ", target)

```

## What the fixed script prints

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

target: <null>
```

## 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: "Trying to cast a freed object.". Find where it comes from and fix it. The variable points at an object that was freed, and as cannot cast what no longer exists. enemy as Node after enemy.free() stops with Trying to cast a freed object. Check is_instance_valid(enemy) before the cast. 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 1637, the message](https://github.com/godotengine/godot/blob/4.7.2-stable/modules/gdscript/gdscript_vm.cpp#L1637).

## Questions

### Is a freed object null?

No. Its variables still hold a reference, and comparing it to null on 4.7.2 is true, but casting it or calling it fails. is_instance_valid() is the check that covers both.

### How do I stop keeping references to freed enemies?

Remove them from your arrays when they die, or connect to the node's tree_exiting signal and drop the reference there.

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: [Left operand of 'is' is a previously freed instance in Godot 4](/godot/left-operand-of-is-is-a-previously-freed-instance/), [Cannot call method '…' on a previously freed instance in Godot 4](/godot/cannot-call-method-on-a-previously-freed-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 cast: can't convert a non-object value to an object type in Godot 4](/godot/invalid-cast-cant-convert-a-non-object-value-to-an-object-type/), [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/)
