Game Development Reference
In-Depth Information
Unity Specifics
Functions
Several essential JavaScript functions need to be included in your script
to talk to Unity. As Unity is running your game, it will look through code
attached to all game objects for functions it needs to execute at any
particular time. For example, the Update() function runs once each main
loop. If there are five game objects each with Update() functions in their
scripts, then all five Update() functions will be executed.
Another useful function is Start() . This function runs just once during the
lifetime of the game object. Think of it as an initialization method for
a game object. You can use it to set initial states and values for a game
object. It does not run over and over again like the Update() function and
therefore is a good place to put code that does not need to run for the
life of the game object. For example, if you want to constantly rotate an
object like that in Listing 1.7 , then it makes sense for the rotation to occur
each loop. However, if you want to set the color of a game object to red,
coding it in an Update() function will just cause the same command to
run over and over again. Once the object is red, there is no need to keep
setting it to red. A more suitable place to make an object red would be in
the Start() function. It is set to red just once.
Remember, code inside Update() runs each main loop and each line of
code in the main loop will cause it to run slower, albeit a minute amount,
but superfluous code can soon add up to huge drops in frame rates.
For Research
More Unity Functions
More Unity-specific functions will be revealed throughout the topic
as needed. If you are interested in other available functions, go to the
Script Reference , type Monobehavior in the search, and have a look at the
Overridable Functions . These are the ones you can use in your JavaScript.
1.5.4 Variables
A variable is the name of a piece of computer memory allocated for your
program. Think of it as a storage box that you can put things into, take things
out of, add to, and change. A variable in programming is similar to a variable
in algebra that holds a numerical value. However, in programming, pretty
much anything can be placed into a variable. In the line of code,
x = 50;
x is the name of the variable and it has a value of 50. If you continued with
y = x + 30;
another variable called y is being given the value of x (50) plus 30. The value
of y in this case would be 80.
Search Nedrilad ::




Custom Search