Skip to content

Why is my identifier not declared in the current scope?

Godot 4.x

You’ll see something like this in the script editor and the Debugger. The identifier name and line will be your own:

Parser Error: Identifier "speed" not declared in the current scope.
What’s happeningGo to
The var line is inside a different functionDeclared inside another function
The var line is below the line that uses it, in the same functionUsed before it is declared
The identifier is an autoload like GlobalAn autoload that is not registered
The identifier is a class_name you just addedA class name the editor has not seen yet
It’s declared right there and still failsA typo or wrong case

The script is on Player.

  • PlayerCharacterBody2Dhas scriptscript attached here

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:

player.gd
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:

player.gd
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()

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:

player.gd
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.

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.

player.gd
func collect_coin() -> void:
Global.score += 1

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.

player.gd
class_name Player
extends CharacterBody2D

Fix: save the script with the class_name. If the error stays, use Project → Reload Current Project so the editor rescans every script.

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:

player.gd
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()

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.