Cannot infer the type of "…" variable because the value is "null" in Godot 4
By Eduardo Orellana · Updated
var target := null asks for the type of null, and null has none to give. The script fails to parse with Cannot infer the type of "target" variable because the value is "null". Name the type and start it at null, var target: Node2D = null, or drop the colon.
AppImage, x86_64. Ubuntu 22.04, Debian 12, Fedora 39 or newer.
Intel or AMD Chromebook, with Linux turned on.
What Godot is checking
Inference with := takes the type of the value on the right. A null literal is Nil, which is not a type a variable can usefully have, so the analyzer refuses the declaration and the script does not load.
Object types can hold null, so var target: Node2D = null is a typed variable that starts empty.
Reproduce it
A target variable declared with := null.
extends Node
func _ready() -> void:
var target := null
print(target)
What Godot 4.7.2 prints
Godot Engine v4.7.2.stable.mono.official.ed1daf0bf - https://godotengine.org
SCRIPT ERROR: Parse Error: Cannot infer the type of "target" variable because the value is "null".
at: GDScript::reload (res://answers/cannot-infer-the-type-because-the-value-is-null/repro.gd:4)
ERROR: Failed to load script "res://answers/cannot-infer-the-type-because-the-value-is-null/repro.gd" with error "Parse error".
at: load (modules/gdscript/gdscript_resource_format.cpp:46)Fix it
Give the type, and null as its starting value.
extends Node
func _ready() -> void:
var target: Node = null
print(target)
target = self
print(target.name)
What the fixed script prints
Godot Engine v4.7.2.stable.mono.official.ed1daf0bf - https://godotengine.org
<null>
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 18 October 2026, and the output above is what Godot printed, unedited.
Where the check lives in Godot's source: [gdscript_analyzer.cpp line 2144, the message](https://github.com/godotengine/godot/blob/4.7.2-stable/modules/gdscript/gdscript_analyzer.cpp#L2144).
Questions
- Can an int variable start as null?
- No. Built-in types such as int, float and Vector2 cannot be null; they start at their zero value. Only Object types and untyped variables can hold null.
- What does the other wording, "the value doesn't have a set type", mean?
- The same check for a value whose type the analyzer cannot work out, such as a call on an untyped variable. Name the type there too.
Related
- The variable type is being inferred from a Variant value
- Trying to assign value of type 'String' to a variable of type 'int'
- Every verified Godot 4 answer
- Trying to return a value of type "…" from a function whose return type is "…" in Godot 4
- Cannot call method '…' on a null value in Godot 4
- Trying to assign a non-object value to a variable of type '…' in Godot 4
- Invalid cast: can't convert a non-object value to an object type in Godot 4
- Cannot get return value of call to "…()" because it returns "void" 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