Quick answer: CS1002 means a statement was not ended with a semicolon, and CS1513 means an opening { has no matching }. The numbers in Player.cs(9,46) are the line and the character where the compiler noticed. For a missing semicolon that is the end of the line that lacks it. For a missing brace it is usually the last line of the file, far from the real mistake, so check that every { above it has a partner. Fix the error with the lowest line number first, save, and let Unity recompile before reading the rest.
These are syntax errors: the code is not valid C# text yet, so the compiler cannot work out what it means. They are the first errors most Unity developers meet, and they are confusing for one reason. The message names the character the compiler wanted, and the position it reports is where it gave up, which is not always where you made the mistake. This guide shows how to read the position, lists the family of syntax errors that travel together (CS1002, CS1513, CS1022, CS1003, CS1026, CS1519 and others), and goes through the causes: missing and extra semicolons, unbalanced braces, code pasted into the wrong scope, and unclosed brackets and quotes. It ends with the editor setup that underlines all of them as you type.
- The exact error
- How to read the position
- The syntax errors that travel together
- Why one mistake prints a dozen errors
- Fix 1: missing and extra semicolons
- Fix 2: unbalanced braces
- Fix 3: code in the wrong place
- Fix 4: brackets, commas and quotes
- Set up your editor so it underlines these
- What Bugnet has to do with syntax errors
- Related guides
- Frequently asked questions
The exact error
Assets/Scripts/Player.cs(9,46): error CS1002: ; expected
Assets/Scripts/Player.cs(19,2): error CS1513: } expected
Each entry is the file path inside the project, the line and column in brackets, the error code and the message. Double-click it in the Console to open the file at that position. Both are compile errors. Unity will not enter Play mode or build a player until they are fixed, and no player ever sees them. If the Console only shows the generic line, see all compiler errors have to be fixed before you can enter Play mode.
How to read the position
In Player.cs(9,46), 9 is the line and 46 is the character on that line, both counted from 1. A tab counts as one character, so the number can differ from the column your editor displays if it expands tabs.
The position is where the parser discovered that something was missing. How close that is to your mistake depends on what is missing.
A missing semicolon is reported at the end of its own line
7 void Update()
8 {
9 float h = Input.GetAxis("Horizontal")
10 transform.Translate(h * speed * Time.deltaTime, 0f, 0f);
11 }
Assets/Scripts/Player.cs(9,46): error CS1002: ; expected
The listing shows line numbers in the margin. In the file, line 9 is indented eight spaces and is 45 characters long, and the error is at character 46: one past the closing bracket, exactly where the semicolon belongs. When the reported column is just beyond the last character of a line, the fix goes at the end of that line. The compiler Unity uses places the error there because the next thing it read was on a new line. If the two statements are on one line, the error points at the first character of the second statement instead.
There is one exception. If the next line can legally continue the expression (it starts with an operator, a dot or an opening bracket), the parser keeps reading and reports the problem later than you expect.
A missing closing brace is reported where the code ran out
7 void Update()
8 {
9 if (Input.GetKeyDown(KeyCode.Space))
10 {
11 Jump();
12
13 }
14
15 void Jump()
16 {
17 Debug.Log("Jump");
18 }
19 }
Assets/Scripts/Player.cs(19,2): error CS1513: } expected
The brace that should close the if is missing from line 12. The compiler has no way to know that. It pairs braces in order, so the } on line 13 closes the if, Jump is read as a function declared inside Update, the } on line 19 closes Update, and the class is still open when the file ends. The error lands on the last line, seven lines below the mistake. For CS1513, treat the reported line as “somewhere above here” and look at indentation, as described in Fix 2.
The syntax errors that travel together
| Code | Message | Usual cause |
|---|---|---|
| CS1002 | ; expected | A statement or field declaration was not ended. |
| CS1513 | } expected | A { was never closed. Reported where the code ran out. |
| CS1022 | Type or namespace definition, or end-of-file expected | One } too many. Reported on the surplus brace, often the last one in the file. |
| CS1003 | Syntax error, ',' expected | A missing comma, bracket or keyword. The character in quotes says which. |
| CS1026 | ) expected | An opening ( was not closed. |
| CS1010 | Newline in constant | A string is missing its closing quote. |
| CS1525 | Invalid expression term ')' | A value is missing inside an expression, as in if (health > ). |
| CS1519 | Invalid token '=' in class, record, struct, or interface member declaration | A statement written directly in the class body, outside any method. |
| CS0116 | A namespace cannot directly contain members such as fields or methods | A method or field outside the class braces. |
| CS0106 | The modifier 'private' is not valid for this item | A method with an access modifier ended up inside another method. |
Two of these read slightly differently depending on the compiler your Unity version ships. CS1519 omits the word record on older versions, and newer compilers shorten it to Invalid token '=' in a member declaration. CS0116 ends in fields, methods or statements on newer compilers. The code number is what to search for.
Why one mistake prints a dozen errors
After a syntax error the parser does not stop. It guesses what you meant, inserts the missing piece in its own picture of the code and carries on, so that it can report more than one problem per compile. When the guess is wrong, everything after it is misread. A string with no closing quote swallows the rest of its line, so the ) and ; that were inside the “string” are reported missing as well. A missing brace puts every later method inside the previous one, and each of those methods that starts with public or private gets its own CS0106.
The working rule follows from that:
- Find the error with the lowest line number in the file. Ignore the others for now.
- Fix that one thing.
- Save, switch back to Unity and wait for the recompile.
- Read the Console again. Most of the list is usually gone.
Chasing the errors from the bottom up, or fixing five at once, tends to add braces and semicolons that were never missing.
Fix 1: missing and extra semicolons
// Broken
public float speed = 5f // CS1002 at the end of this line
int max health = 100; // CS1002: names cannot contain spaces
do { timer -= 1f; } while (timer > 0f) // CS1002: do-while ends with a semicolon
// Fixed
public float speed = 5f;
int maxHealth = 100;
do { timer -= 1f; } while (timer > 0f);
Every statement and every field declaration ends with a semicolon. Lines that open a block do not: no semicolon after a class header, a method header, if (...), for (...), while (...) or else, and none is needed after a closing brace. The compiler says ; expected whenever a statement could have ended and something else follows, so the cause is sometimes an extra word or a typo and not a forgotten semicolon. Read the text just before the reported character.
A semicolon in the wrong place is the opposite mistake, and it is more dangerous because only one of the three cases below is an error:
void Update(); // CS0501: 'Player.Update()' must declare a body because it is not marked abstract, extern, or partial
{ // CS1519: Invalid token '{' ...
Move();
}
if (health <= 0); // compiles; warning CS0642: Possible mistaken empty statement
{
Die(); // runs every time, whatever health is
}
while (timer > 0f); // compiles; if timer is positive this never ends
{
timer -= Time.deltaTime;
}
A semicolon straight after the brackets of if, for or while is a complete, empty statement, and the block below it is no longer attached to the condition. The while version loops forever on the empty statement and freezes the editor. Check the Console’s warnings for CS0642 after fixing syntax errors.
Fix 2: unbalanced braces
Braces come in pairs, and each } closes the nearest { that is still open. Three outcomes are possible when the count is off.
- One closing brace missing: CS1513 on the last line of the file, as in the example above. If the methods below the mistake have access modifiers, you also get CS0106 on each.
- One closing brace too many: the class ends early. The surplus
}is reported as CS1022, and methods that are now outside the class are reported as CS0116 or, on compilers that support C# 9, as CS8803 (Top-level statements must precede namespace and type declarations.). - One missing and one extra: the counts balance, the file compiles, and a method sits inside another method. See the next heading.
To find the spot, let the editor show you the structure:
- Run the editor’s format command: Edit > Advanced > Format Document in Visual Studio, Format Document from the Command Palette in VS Code, Code > Reformat Code in Rider. Code is re-indented by brace depth. The first method that sits one level deeper than its neighbors comes right after the missing brace.
- Click just after a brace. The editor highlights its partner. A method’s closing brace that pairs with an
ifor a loop is the mismatch. - Collapse methods with the fold arrows in the margin. If collapsing one method hides the methods below it as well, that method is the one missing its closing brace.
Then add the brace where the block should end. Do not add it at the line the error names. A } appended to the end of the file silences CS1513 and leaves every method nested in the wrong place.
A method inside another method compiles
void Update()
{
Move();
void OnTriggerEnter(Collider other) // a local function inside Update
{
Destroy(other.gameObject);
}
}
C# allows functions inside methods, so this is legal as long as the inner one has no public or private in front of it. Unity only calls message methods that belong to the class, so this OnTriggerEnter never runs and nothing in the Console’s error list explains why. The compiler does leave a warning, CS8321: The local function 'OnTriggerEnter' is declared but never used. If a callback silently stopped firing after you edited braces, look for that warning and move the method out to class level.
Fix 3: code in the wrong place
// Broken
public class Spawner : MonoBehaviour
{
public GameObject prefab;
float timer;
timer = 2f; // CS1519: Invalid token '=' in class, record, struct, or interface member declaration
}
// Fixed
public class Spawner : MonoBehaviour
{
public GameObject prefab;
float timer = 2f; // an initial value on the declaration is fine
void Start()
{
timer = 2f; // or assign inside a method
}
}
A class body may contain declarations only: fields, properties, methods, nested types. Anything that does something (an assignment to an existing variable, a method call such as Debug.Log, an if, a loop) must be inside a method. A call written in the class body produces CS1519 followed by a cluster of other syntax errors, because the parser tries to read it as a declaration.
The scopes of a script nest in a fixed order, and pasted code has to land in the right one:
| Location | What may go there | Error if you put something else there |
|---|---|---|
| Top of the file | using lines | CS1529 for a using placed after a class |
| Outside any class | Classes, structs, enums, interfaces, namespaces | CS0116 or CS8803 for a method or field |
| Inside the class braces | Fields, properties, methods, nested types | CS1519 for a statement |
| Inside a method’s braces | Statements and local variables | CS0106 for a method with an access modifier |
When a tutorial shows a complete script, replace the entire contents of your file with it. When it shows a single method, paste it after the closing brace of another method and before the final brace of the class. If you paste a full class into a file that already contains a class of that name, the syntax is valid and you get duplicate definitions instead, which is CS0101 and CS0111.
Fix 4: brackets, commas and quotes
// Broken
if (Input.GetKeyDown(KeyCode.Space) // CS1026: ) expected
{
Jump();
}
Debug.Log("Jumped); // CS1010: Newline in constant, then CS1026 and CS1002
Vector3 p = new Vector3(1f 2f, 3f); // CS1003: Syntax error, ',' expected
if health <= 0) // CS1003: Syntax error, '(' expected
{
Die();
}
// Fixed
if (Input.GetKeyDown(KeyCode.Space))
{
Jump();
}
Debug.Log("Jumped");
Vector3 p = new Vector3(1f, 2f, 3f);
if (health <= 0)
{
Die();
}
Round brackets pair up the same way braces do, and a nested call needs one closing bracket per opening one: if (Input.GetKeyDown(KeyCode.Space)) ends with two. CS1003 is the general “I expected this character here” error. Read the character in quotes and look immediately before the reported position. The three parts of a for header are separated by semicolons, not commas, and method arguments by commas, not spaces.
An unclosed string is the clearest example of follow-on errors. The compiler reads to the end of the line looking for the closing quote, reports CS1010 when the line ends, and then reports the bracket and semicolon it never saw. Add the quote and all three disappear.
Set up your editor so it underlines these
Every error in this guide is one the code editor can mark with a red underline as you type, before you switch back to Unity. If yours does not, it is not connected to the Unity project.
- In Unity, open Edit > Preferences > External Tools (on macOS the same window is under the Unity menu).
- Set External Script Editor to the editor you use: Visual Studio, Visual Studio Code or Rider.
- Click Regenerate project files.
- Open scripts by double-clicking them in Unity’s Project window, so the editor loads the whole project and not a single loose file.
Each editor needs its own piece installed:
- Visual Studio needs the Game development with Unity workload, selected in the Visual Studio Installer.
- Visual Studio Code needs Microsoft’s Unity extension, which installs the C# Dev Kit with it, and Unity’s Visual Studio Editor package up to date in Window > Package Manager (under Window > Package Management in Unity 6). The older Visual Studio Code Editor package is no longer maintained.
- Rider needs the JetBrains Rider Editor package, also in the Package Manager.
A quick test: delete a semicolon and wait a second. A red underline at the end of the line means the setup works. In Visual Studio, a file tab that shows Miscellaneous Files in the project dropdown is a loose file with no project behind it, and it gets no error checking.
Two habits prevent most of these errors. Let the editor type the closing brace, bracket and quote for you (all three editors insert the partner automatically), and type the pair first, then fill in the middle.
What Bugnet has to do with syntax errors
Nothing directly, and it is worth being plain about that. A syntax error stops compilation on your machine. No build exists, nothing runs, and no report can be filed by Bugnet or any other tool that lives inside the game. The connection is to the two cases above that compile. An if with a stray semicolon runs its block every time, and a callback that slid inside another method never runs at all. Neither throws an exception or writes an error to the log, so automatic capture has nothing to see.
Those arrive as behavior: a door that never opens, damage applied on every frame. The Bugnet Unity SDK adds an in-game report form, and a player’s report comes with the game version, platform, OS and the recent log lines attached, which tells you which build misbehaves even though it cannot point at the line. Errors the game does log, and exceptions, are captured automatically and stacked by title. For the silent kind, the compiler warnings CS0642 and CS8321 on your own machine are the earlier and cheaper signal. The bug reports guide shows what a filed report contains.
Related guides
- How to Fix “All compiler errors have to be fixed before you can enter Play Mode” in Unity
- How to Fix “error CS0101: The namespace already contains a definition for” and CS0111 in Unity
- How to Fix “error CS0103: The name does not exist in the current context” in Unity
- How to Fix “The associated script cannot be loaded” in Unity
- 12 Common Unity Errors and How to Fix Them
Frequently asked questions
What does error CS1002 ; expected mean in Unity?
The compiler reached a point where a statement should have ended and found no semicolon. The reported column is usually one past the last character of the line that needs it. Add the semicolon there, save and let Unity recompile.
Why does CS1513 } expected point at the last line of my script?
Braces are paired in order, so when one closing brace is missing every later brace closes the wrong block and the class is still open when the file ends. The compiler reports the problem where it ran out of code. The missing brace is somewhere above that line, usually at the end of an if, a loop or a method.
What do the numbers in brackets after the file name mean?
They are the line and the character position on that line, both counted from 1, where the compiler noticed the problem. Double-clicking the Console entry opens the script at that position.
Why do I get so many errors from one missing brace or quote?
After a syntax error the compiler guesses what was meant and keeps reading, and a wrong guess makes it misread the code that follows. Fix the error with the lowest line number, save, and recompile. Most of the remaining errors usually disappear with it.
What does CS1022 Type or namespace definition, or end-of-file expected mean?
There is one closing brace too many, so the class ended before you intended and a later brace has nothing to close. Find the surplus brace, which is often above the line the error names, and delete it.
The position in a syntax error is where the compiler gave up, not always where you slipped. Fix the first error in the file, recompile, and only then read the rest of the list.