Common Errors
Overview: Compiler Errors and Runtime Errors
There are two types of errors that can happen in a program:
- Compiler Errors are issues with your code that prevent the game from running at all. These are often caused by Syntax Errors: errors in the spelling and grammar of your code.
- Runtime Errors are issues with your code that cause unexpected problems while the game is running. You will still be able to run a game that has runtime errors, but things might not work as you expect.
All compiler errors have to be fixed before entering playmode!
If you try to play your game, and see this message, it means that you have Compiler Errors that are preventing unity from running your game. These kind of errors will always create a red error message in the console window. Look at the Console window and fix any red error messages (see below)
The game runs but something is broken!
If you are able to run the game but something isn’t working as you expect, that means you likely have some kind of runtime error. Sometimes (but not always!) a runtime error will create a red error message when the error occurs (i.e. while the game is running).
If something in your game is broken, the console is the first place you should look for a hint.
How to Find and Fix Errors
- Look at your console window (Window>General>Console)
- Find any red error messages.
- Read the error message carefully. It will give you clues about what is going wrong.
- Error messages always show what line the error occurs on, and which character the error appears at in that line.
This error occurs in the file MoveCharacter.cs, on line 13, at character 17
use the line numbers on the left hand side to find where your error is
- Double click on an error and visual studio will open that script and show you what line the error occurs on.
- This mostly works well but if you have a lot of errors it can get messed up. Start with the first error in the console and work your way down.
The Most Common Compiler Errors
; expected
Somewhere in your script, you are missing a semicolon (;)
Each statement you make in a script must be followed by a semicolon; think of it as the coding equivalent of the period (.)
EXCEPT never put a semicolon after curly braces {}
The name X does not exist in the current context
This means you are trying to use a variable without declaring it first.
Here, I am trying to assign the value 5 to a variable called health, which I have not declared.
Before I can use a variable, I must declare it.
In the example above on line 10, I declare an integer variable called health by writing int health
and assign a starting value by adding =0
.
On line 11, I am then able to refer to the variable by only its name.
Here, I declare the integer health on line 7, outside of any function. This allows me to use health in any function within this script.
} expected
Remember that every open bracket {
MUST HAVE a corresponding closing bracket }
.
These types of errors can sometimes be tricky to fix because the error message won’t always point you to the right place in your code.
Notice that the error message says the error occurs on line 18. This is because line 18 is where the compiler got to before it realized there was a problem.
When I open Visual studio, I see a red squiggle after the }
on line 18
Adding another }
to line 18 will make the error go away, but our script won’t work as intended.
Instead of having a separate Start and Update function, Unity now thinks we want to have a Start function… with a function called Update declared inside of it! This is a very weird thing to do.
In this case, the opening bracket {
on line 9 after void Start()
is missing a corresponding }
closing bracket.
I should add a closing bracket after the open bracket on line 9.
The Most Common Runtime Errors
UnassignedReferenceException: The variable <variable name> of <script name> has not been assigned.
You probably made a public variable and tried to use it without assigning it.
An empty field looks like this:
Make sure to assign public variables in the editor by either:
- clicking and dragging a game object with the component type into the field
- clicking the dot at the right side of the empty field and browsing for the object you want
- NOTE that you can search “In Scene” or “In Project” - in project refers to prefabs etc in your project panel, in scene refers to objects in the current scene.
ArgumentNullException: Value cannot be null.
You will see this error if you use GetComponent to assign a variable, and GetComponent returns “null”, because it couldn’t find a component of the type you specified.
In the above example, on line 12, if GetComponent<AudioSource>()
does not find an AudioSource component attached to the game object, the variable aSource is assigned a null value. In that case, I get the error when I try to call aSource.Play();
because there isn’t **any audio source to make play!
Here on line 14, I use an if statement to check if aSource is null before trying to make the audiosource play. If the audiosource is null, then it is ignored.
‘Debug’ is an ambiguous reference between ‘UnityEngine.Debug’ and ‘System.Diagnostics.Debug’
This error usually means that visual studio has automatically added a line near the top of your script (probably line 3 or 4) which says “using System.Diagnostics;”
Delete this line.
Visual Studio 2022 can be very insistent about adding references to things that might cause problems later on. If your build is failing, this might be one of the reasons!