error CS0103: The name does not exist in the current context
Console
error CS0103: The name 'X' does not exist in the current context
You used a name, usually a variable, that is not declared where the code can see it. C# looks for names in the current block, then the method, then the class.
Read the Console line first
X is the name C# could not find. The file and line come before the message.
Common causes
The variable was declared inside another block
A variable declared inside an if, a loop or another method does not exist outside it. Declare it at class level to share it between methods.
The name is misspelled
moveSpeed and movespeed are different names.
A static helper is called without its class
Mathf.Clamp, not Clamp, unless you add using static UnityEngine.Mathf;.
The fix
Fix
C#
public class Jumper : MonoBehaviour
{
private bool isGrounded; // class level: visible to every method
void Update()
{
if (isGrounded && Input.GetButtonDown("Jump")) Jump();
}
}Stop it coming back
Keep fields at the top of the class and give locals short, obvious scopes.
Questions
- I declared the variable in Start. Why can't Update see it?
- A variable declared inside a method only exists inside that method. Declare it as a field in the class body, outside every method, and assign it in Start; then every method in the class can use it.
- Why does Instantiate or Destroy not exist in my class?
- Those methods belong to UnityEngine.Object, which MonoBehaviour inherits from. In a plain C# class, call them through the class:
Object.Instantiate(prefab)andObject.Destroy(obj), withusing UnityEngine;at the top. - Is C# case-sensitive?
- Yes.
playerHealthandPlayerHealthare different names. Unity's own APIs use PascalCase for methods and properties, such astransform.positionandTime.deltaTime.
Related errors
Got a different error? Paste it into the error lookup.