Game Development Reference
In-Depth Information
If you are already a programmer, by the way, you can use an editor of your own choosing by
accessing the External Tools panel in Unity's preferences from the Edit menu.
Writing Scripts
With a bit of general information about scripts under your belt, you are probably ready to learn
how to start filling them up with code. Basic scripts usually have three types of content: variables,
functions, and comments. Variables are the means of storing information that is used and often
changed throughout your game. Functions are where all the action happens. Both variables and
functions come in several varieties. The third type of content is comments . Comments allow you
to make notes about your code so you or someone else can decipher what the code is meant to
accomplish. Comments, even though they do not affect your script's functionality, are an integral
part of coding, so rather than leave that topic until last, you will take a look at them before jumping
into functions.
Introducing Variables
Now that you have a script, it's time to start poking at it to see what it can do. A class, being a sort
of template, quite often contains variables . These are parameters that help you store important
values, help you keep track of states, and allow for the customization of the gameObject the script is
applied to.
There are a few rules to follow when naming variables. Variables cannot be reserved words. (Those
will appear blue in the MonoDevelop editor.) They cannot start with numbers or certain special
characters. They may not contain spaces. And, in Unity, you should always start your variable names
with lowercase letters.
Variables come in several different “types.” Some types are generic and can be found in most
scripting languages, and others are Unity specific.
Defining Variable Types
Variables have types. In Unity's version of JavaScript, if you do not declare the type of variable, it
will do its best to try to figure out what type of variable it is. Not only is this a slow process, it is also
not allowed in most mobile platforms. In C#, the type must be declared when the variable is first
introduced into the code. The most common types are numbers, strings, and Booleans.
A classic example using various variable types is a script, or class, for describing an animal. It might
have a parameter for blood type (warm or cold), number of legs, type of teeth, coat type, or any
other number of parameters that let you define a particular type of animal.
So the first thing you have to do with your variable is to decide what type it is.
Numbers come in a few different types. Integers are whole numbers that can be negative or positive.
Fractional numbers come in several different varieties in C#. Floats or floating point numbers are
32-bit, floating-point values. Doubles store 64-bit, floating-point values. Integers and floats are the
most commonly used types of numbers in Unity.
 
Search WWH ::




Custom Search