error CS1002: ; expected

ConsoleCompiler error · Scripting

error CS1002: ; expected

A statement ended without a semicolon, so the compiler ran into the next token and stopped. It is the most common syntax error in C#, and often comes with a few follow-on errors that disappear once it is fixed.

Read the Console line first

The numbers after the file name are line and column, such as Player.cs(14,30). The compiler reports where it expected the semicolon, which is often the end of the line above the one you would look at first.

Common causes

  1. A statement is missing its semicolon

    Every statement in C# ends with a semicolon: declarations, assignments and method calls. Lines that end with a brace, such as if blocks and method bodies, do not need one.

    Example
    C#
    float speed = 5f
    rb.linearVelocity = direction * speed; // CS1002 reported at the end of the line above
  2. A parenthesis or bracket is missing earlier

    An unclosed parenthesis makes the compiler read the next lines as part of the same expression, and it reports a missing semicolon where it finally gives up.

  3. Code was pasted with smart quotes or stray characters

    Quotes copied from a web page or document can be curly instead of straight. The compiler then reports unexpected characters (CS1056) and often a missing semicolon after them. Retype the quotes in your editor.

The fix

Fix
C#
float speed = 5f;
rb.linearVelocity = direction * speed;

Stop it coming back

Let your IDE format the file (it shows syntax errors as you type, before Unity compiles), and fix the first error in the Console first: later errors are often caused by it.

Questions

Why does fixing one semicolon clear ten errors?
One syntax error confuses the parser for the rest of the file, so it reports follow-on errors that are not real. Always fix the first error in the Console list first.
Do if statements and loops need a semicolon?
Not after the closing brace. A semicolon directly after if (condition); is legal but ends the if immediately, so the block below always runs, a silent bug rather than an error.
Does Unity show syntax errors before compiling?
Unity only reports them after it compiles. Your IDE (Visual Studio, Rider or VS Code with the C# extension) underlines them as you type.

Scripts that avoid it

Related errors

Got a different error? Paste it into the error lookup.