Skip to content

Why do I get "Unexpected indentation"?

Godot 4.x

You’ll see something like one of these in the script editor and the Debugger. The line number will be your own:

Parse Error: Unexpected indentation.
Parse Error: Expected indented block after function declaration.
Parse Error: Used space character for indentation instead of tab as used before in the file.
What’s happeningGo to
The error mentions space character and you pasted code from somewhereMixed tabs and spaces
The error mentions Expected indented blockAn empty function or if block
The error says Unexpected indentationA line indented when it should not be
The error is something else and points at the end of a functionCode outside the function it was meant for

The script is on Player.

  • PlayerCharacterBody2Dhas scriptscript attached here

GDScript uses indentation instead of braces to say which lines belong to a function or an if. That makes the exact whitespace at the start of each line part of the code, so one wrong tab breaks the script.

Godot wants every line in a file indented the same way. Your own lines use tabs, but code pasted from a web page usually arrives with spaces, and the first pasted line that mixes them gets the space character error. Both lines below look identical in the editor:

player.gd
func _physics_process(delta: float) -> void:
velocity.x = speed
move_and_slide()
move_and_slide()

Fix: in the script editor menu, choose Edit → Convert Indent to Tabs. To see the difference next time, turn on Editor → Editor Settings → Text Editor → Appearance → Show Whitespace. Tabs show as arrows and spaces as dots.

A func, if, for or match line ends with a colon and promises indented lines underneath. If nothing is there yet, Godot reports Expected indented block:

player.gd
func take_damage(amount: int) -> void:
pass
func _physics_process(delta: float) -> void:
move_and_slide()

Fix: put pass in the block until you write the body. It does nothing and keeps the parser happy.

Every line has to be at the level of the code around it. A stray tab on a line after a function has ended, or a var at the top of the script that got indented, produces Unexpected indentation:

player.gd
extends CharacterBody2D
var speed := 300.0
var speed := 300.0
func _physics_process(delta: float) -> void:
velocity.x = speed
move_and_slide()

Fix: un-indent the line to match its neighbours. Top-level var, func and signal lines start at column one. Lines inside a function start with one tab.

Code outside the function it was meant for

Section titled “Code outside the function it was meant for”

The opposite mistake gives a different error. If the last line of a function loses its tab, it becomes top-level code, and Godot complains with Unexpected "Identifier" in class body instead:

player.gd
func _physics_process(delta: float) -> void:
velocity.x = speed
move_and_slide()
move_and_slide()

Fix: indent it back into the function. When an error points at a line just after a function, check the indent of the line above it too.

The error names the line. Click it in the Debugger to jump straight there, turn on Show Whitespace, and compare the leading characters on that line with the line above. They should be the same symbols, the same number of times, unless the line above ends with a colon, in which case this line needs exactly one more tab.