🤔 Unity, huh, how?

🤔

CS1002

; expected

Resolution

This error occurs in two situations:

First, check if your statement requires a semicolon.

What statements need semicolons?

🔴 Don't use a semicolon

Statements followed by a block are the common place to exclude a semicolon, blocks are often defined by their opening and closing braces, { and }, which can sometimes be omitted in single-line cases.

warning

If you use a semicolon after a block statement it will become an empty statement, and the following block will be disconnected from the preceding statement, causing it to do nothing.

if (example == null); // <- extra semicolon (;)
{
    Debug.Log($"{nameof(example)} is null!"); // This will always print because the previous selection statement has a semicolon!
}

If you are confused, look at example code to see if yours needs a semicolon.

Adding a semicolon

Add a semicolon ; to the end of your line.
If all errors on that line go away, it's likely the correct solution. Otherwise you may have an incorrectly written statement.

Example

public void Start()
{
    transform.position = _targetPosition;
    transform.rotation = _targetRotation  // Missing semicolon, add a ; here.
}
information

If your error isn't underlined in red you must configure your IDE.

Fixing an incorrectly written statement

Statements can be written incorrectly in extremely varied ways. Compare your code structure with a functioning example to see what you're missing.

Whitespace in C#

Whitespace (spaces, tabs, and newlines) is not significant in C#, functionally a piece of code can have all tabs and newlines removed and it will still run1, you can also add extra newlines to make multi-line statements. We instead require a terminator to tell the compiler where a statement begins and ends.

  1. Spaces may be significant when separating keywords and names. Whitespace is significant inside of strings and characters.