Jul28
2014

By: Elyse                Categories: BooksGames

No matter how much experience you have with coding, there are some errors that will drive you insane and take many minutes if not hours to debug. While logical errors will allow a program to run but function oddly, syntactical errors will stop you dead in your tracks as the program will refuse to compile. The former requires a lot of play testing and code tracing to debug, the later just requires your own understanding of proper coding syntax. If your program will not compile here are the ten most likely common errors.

1. No semicolon at the end of a statement

If you leave a semicolon off the end of a statement, the compiler will consider the next line to be part of the same statement. This will cause compiler errors. Errors will appear in the Console window and at the bottom of the Editor window with a red exclamation mark as shown in Figure 2.7.

When a semicolon is missing the error does not necessarily reflect the exact line where the semicolon is needed. You will need to inspect the code above and around the error to locate the missing statement end marker.

FIG 2.7 Errors in Unity

2. A missing matching parenthesis

Parentheses, brackets, or braces are used to define the start and end of a block of code, the container for logic statements or function variables, or order of precedence in mathematical and Boolean algebra.A pair of parentheses can actually be used in the middle of many lines of procedural code without consequence, although very few programmers practice this, however whenever you include one opening bracket you must include a closing one. For example,

is missing a matching brace near the end of line 2 and line 3 has a closing parentheses but no matching opening one.

3. A line break inside a string

A string is data defined inside two double quotes. Putting a new line inside these quotes will cause a compiler error. For example,

is not legitimate. If the text inside the string actually wraps because of editor formatting, this is fine. However, actually typing a return inside a string will fail. If you must break up a large string into smaller pieces they can be concatenated with a plus sign thus:

4. The wrong casing

Programming compilers are very fussy about casing. They consider the variable myScore and MyScore to be two very different values. If a variable name or function has the wrong casing, such as

then most compiler errors will be similar to

5. The wrong spelling

The wrong spelling of a variable or function name produces the same error as incorrect casing. These two syntactical errors are often very difficult to locate and you need to take note of the compiler error to identify where your typing mistake is.

6. The use of a reserved word for a variable or class name

Sometimes and by complete accident you will choose a reserved word as a variable or function name. A reserved word has special meaning in code and will cause a compiler error if used incorrectly. For example, in C#, interface is a reserved word and hence

produces the error

7. The filename doesn’t match the class name

As previously discussed, the filename of a C# script must match that of the code defining the class. If it doesn’t then you won’t receive an error from Unity until you try and use the script by attaching it to an object. When you do, an error window will open as shown in Figure 2.8, making it clear what you have done wrong.

8. A float has been initialized without an “f” on the end

C# is particularly sensitive to float declaration. If you were to declare

the error

Figure 2.8

would come up. Without a trailing “f” in the initialization, for example,

the compiler considers the value 5.687 to be a double. As a double value takes up a larger space in memory than a float, the compiler won’t allow this type of operation. However, don’t make decimal numbers doubles just to save yourself the errors of forgetting an “f” as this will make your program less efficient to the larger amounts of memory you are manipulating.

9. The wrong type of value is being assigned to a variable

This error is similar to the previous one. If a variable is declared as an integer, then you cannot assign a float value to it. Similarly you can’t store a string in a double. If you were to try

you’d receive the error:

Implicit conversion means trying to make one datatype directly into another, usually using an equals sign. It won’t work.

10. The variables being passed to a function are not the correct datatype

This error is caused by the same issue as the previous one. In passing a value into a function it is being assigned to a variable within that function. If the value the function is expecting is not the same as the value being passed though you will get an implicit conversion error.

For example, if we try and instantiate a new Vector3 and give it the wrong datatypes for the parameters, for example,

The first informs us we have used the method incorrectly and actually shows us what the usage with the variable types should be and the second is basically an implicit conversion error. In this case we’ve tried to pass two double values and a string to the Vector3 constructor when it’s actually expecting three floats.

______________________________

Excerpt from Holistic Mobile Game Development with Unity by Penny de Byl © 2014 Focal Press an imprint of Taylor & Francis Group. All Rights Reserved.

No Comments

No Comments

Tell us what you think!