Game Development Reference
In-Depth Information
tutorials we will use the default UniSciTE and Unitron; although give some of
those other editors a look—they do some great things.
When scripts are created or edited in the editor, they are just text. When the
script is saved (back into the Assets folder), Unity will take a look at the text and
attempt to compile the script—this means it checks to see if it understands the
script. If there are problems, they will show up in the Console and the game
will not run until the errors (mostly syntax) have been corrected.
Console
The Console is actually built into Unity, but is not (by default) shown (although
the last line of the Console is visible at the bottom left corner of the interface).
It's accessible via Window>Console. The Console can provide some important
information. First, if there are errors in a script, the error will be listed in the
Console. The Console will even attempt to let you know which line of which script
has the problem. None of the errors can be corrected in the Console, but after
fixing the problems, the Console will let you know all's well by not complaining.
Additionally, the Console can be used to report what's happening in a script.
Scripts can include commands that print values, or list what's called a (Debug.
Log) that can help track where a script was fired in game, or what Unity thinks
the values of a particular variable are. Being able to see the Console while
creating script is critical. Be sure it is visible (Window>Console), and docked
within the interface. To dock it, just drag the Console tab to where you wish
the Console to reside within the Unity interface.
What Is a Script?
A script contains commands (statements), organized in blocks and functions,
as described next.
Commands and Statements
At its core, scripts are collections of instructions or commands (sometimes
also called statements ). In JavaScript, each command ends with a semicolon.
Without a semicolon, Unity will throw an error and require you to go back and
add that bit of punctuation before it will even bother reading the statement.
An example of a JavaScript command can be as simple as:
height = 6;
Blocks
A collection of commands are called blocks of code. JavaScript recognizes a
block of commands by { and }. It's important that for every single { there is a }.
Again, without this piece of punctuation, Unity won't bother trying to listen to
what you're saying. See the functions example next for how this works.
Functions
Commands can be just floating around within a script, but generally, blocks
of commands are grouped within a function . Grouping commands within a
Search WWH ::




Custom Search