Why do I get "Unexpected indentation"?
The error
Section titled “The error”You’ll see something like one of these in the script editor and the Debugger. The line number will be your own:
Which cause is it?
Section titled “Which cause is it?”| What’s happening | Go to |
|---|---|
The error mentions space character and you pasted code from somewhere | Mixed tabs and spaces |
The error mentions Expected indented block | An empty function or if block |
The error says Unexpected indentation | A line indented when it should not be |
| The error is something else and points at the end of a function | Code outside the function it was meant for |
The example scene
Section titled “The example scene”The script is on Player.
PlayerCharacterBody2D
script 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.
Mixed tabs and spaces
Section titled “Mixed tabs and spaces”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:
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.
An empty function or if block
Section titled “An empty function or if block”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:
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.
A line indented when it should not be
Section titled “A line indented when it should not be”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:
extends CharacterBody2D
var speed := 300.0var 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:
func _physics_process(delta: float) -> void: velocity.x = speedmove_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.
Still stuck?
Section titled “Still stuck?”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.