Debugging is generally quite frustrating and painstaking. And as you start scripting filled with enthusiasm and compiling your first lines of codes with this burning desire to see what it does when you run it, this energy can quickly be tamed down as the console windows returns undecipherable errors that you find it difficult to understand. As time goes on, and these errors remain after inspecting your code for the 100th times, frustration starts to set in and you may just decide to leave the project for a while or just close down the computer and chill in front of your favorite movie/series and relax. But wait…..We have all been there. There is light at the end of the tunnel and some simple tips and principles you can follow to decode these error message. So let’s look at five tips that can help you to move-on and get this script working.
When an error occurs, Unity usually provides you with enough information to check where it has occurred, so that you can fix it. While many are relatively obvious to spot, some others are trickier to find. In the following, I have listed some of the most common errors that you will come across as a budding Unity coder. The trick is in recognizing the error message to understand what Unity is trying to tell us.
Again, this is part of the learning process, and you WILL make these mistakes, but as you see these errors, you WILL also learn to understand them (and avoid them too). Again, Unity is trying to help by communicating to best that it can where the issue is, and by understanding its language, we can get to fix these bugs easily. So here they are…..:
- “;” expected: This error means that you have forgotten to add a semi-colon at the end of a statement. To fix this error, just go to the line concerned and ensure that you add a semi-colon. It can also be due to the fact that you could have forgotten the keyword function as you defined a function.
- Unknown identifier: This error means that unity does not know the variable or the function that you are mentioning. It can be due to at least three reasons: (1) the variable/function has not been declared, (2) the variable/function has been declared but outside the scope of the function (e.g., declared locally in a different function), or (3) the name of the variable/function that you are using is incorrect (i.e., spelling or case). Remember, the name of variables or functions are case-sensitive; so by just using an incorrect case, Unity will assume that you refer to another variable, that, in this case, has not been declared yet.
- The best method overload for function … is not compatible: This error is probably due to the fact that you are trying to call a function and pass a parameter with a type that is not what it is expecting. For example if your function expect a String parameter and an integer is passed, an error will be generated.
- Expecting } found …: This error is due to the fact that you may have forgotten to either close or open brackets. This can be the case for conditional statements, or functions. To avoid this issue, there is a trick or best practice that you can use: you can ensure that your indent your code so that corresponding opening and closing brackets are at the same level. In the next example for example, you can see that the brackets corresponding to the start and end of the function are indented at the same level, and so are the brackets for each of the conditional statements. By indenting your code (using 2-3 characters or a tabulation), you can ensure that your code is clear.
function testBrackets() { if (myVar == 2) { print ("Hello World”); myVar = 4; } else { } }
- No errors displayed but the script is not working (“Nothing is happening”): Sometimes, although the syntax of your code is correct and does not yield any error in the console window, it looks like nothing is happening, in other words, it looks like the code, and especially the functions that you have created do not work. This is bound to happen as you create your first scripts. It can be quite frustrating (and I have been there ☺) because, in this case, Unity will not let us know where the error is. However, there is a succession of checks that you can perform to ensure that this does not happen; so you could check the following:
- The script that you have written has been saved
- The script has no errors
- The script is attached to an object
- If the script is indeed attached to an object and you are using a built-in function that depends on the type of object it is attached to, make sure that the script is linked to the correct object. For example, if your script is using the built-in function OnControllerColliderHit, which is used to detect collision between the First-Person Controller and other objects, but you don’t drag and drop the script on the FPSCOntroller object, the script while active will run if you collide with an object
- If the script is indeed in attached to the right object and is using a built function such as Start, or Update, make sure that these functions are spelt properly (i.e., exact spelling and case). For example for the function Update, what happens here is that the system will call the function Update every frame Not any other function. So if you write a function spelt update, the system will look dor the Update function, and since it has not been defined (more precisely, overwritten), nothing will happen. The same would happen for the function Start. In both cases, the system will assume that you have created two new functions update and start.