Why is my identifier not declared in the current scope?
The error
Section titled “The error”You’ll see something like this in the script editor and the Debugger. The identifier name and line will be your own:
Which cause is it?
Section titled “Which cause is it?”| What’s happening | Go to |
|---|---|
The var line is inside a different function | Declared inside another function |
The var line is below the line that uses it, in the same function | Used before it is declared |
The identifier is an autoload like Global | An autoload that is not registered |
The identifier is a class_name you just added | A class name the editor has not seen yet |
| It’s declared right there and still fails | A typo or wrong case |
The example scene
Section titled “The example scene”The script is on Player.
PlayerCharacterBody2D
script attached here
Declared inside another function
Section titled “Declared inside another function”A variable declared inside a function only exists inside that function. Here speed is created in _ready(), so _physics_process() has never heard of it:
extends CharacterBody2D
func _ready() -> void: var speed := 300.0
func _physics_process(delta: float) -> void: velocity.x = Input.get_axis("left", "right") * speed move_and_slide()Fix: move the var to the top of the script, outside any function. Then every function in the script can use it:
extends CharacterBody2D
var speed := 300.0
func _ready() -> void: var speed := 300.0
func _physics_process(delta: float) -> void: velocity.x = Input.get_axis("left", "right") * speed move_and_slide()Used before it is declared
Section titled “Used before it is declared”Inside a function, lines run top to bottom, and a variable doesn’t exist until its var line has run. Using it one line too early fails:
func count_coins() -> void: print(total) var total := 0 print(total)Fix: declare first, use after. This only matters inside functions. Variables at the top of the script can be declared in any order, above or below the functions that use them, and Godot finds them fine.
An autoload that is not registered
Section titled “An autoload that is not registered”Global.score works only if a script is registered as an autoload named exactly Global. Open Project → Project Settings → Globals (the Autoload tab) and check the list. If the script isn’t there, add it. If it’s there under a different name, like Globals or global, either rename it or change your code to match. The name is case-sensitive.
func collect_coin() -> void: Global.score += 1A class name the editor has not seen yet
Section titled “A class name the editor has not seen yet”Right after you add class_name Player to a script, other scripts may still fail on var p: Player. The editor only picks up new class names when the script is saved.
class_name Playerextends CharacterBody2DFix: save the script with the class_name. If the error stays, use Project → Reload Current Project so the editor rescans every script.
A typo or wrong case
Section titled “A typo or wrong case”Identifiers are case-sensitive and exact: speed, Speed and spped are three different names. Copy the name from its var line instead of retyping it:
var speed := 300.0
func _physics_process(delta: float) -> void: velocity.x = Input.get_axis("left", "right") * Speed velocity.x = Input.get_axis("left", "right") * speed move_and_slide()Still stuck?
Section titled “Still stuck?”The error gives you the exact identifier and the line number. Press Ctrl+F in the script editor and search for var speed (with your own name). If it turns up nothing, it’s a typo or a missing autoload. If it does turn up, look at which function that var line is inside and compare it with the function the failing line is in. If they differ, that’s your answer.