Game Development Reference
In-Depth Information
Functions always have this syntax:
function myFunction() {
}
In this sample, I typed function to tell the computer it is defining a function. Then
I named the function by giving it a custom name, MyFunction . At this point I could also
have specified a standard function type. Next I typed a closed set of parentheses. These
parentheses are empty, as is the usual case; alternatively, this is where to put information
about variables that the function should be referencing, such as collider or numerical
data, from other scripts. Finally, I typed a curly bracket to start the commands that make
up the function and another curly bracket to end the commands. All directions given
inside the function must go within these curly brackets, as they tell the computer when
the function starts and stops.
Like variables, functions can be named and referenced for their uses. However, there
are many types of standard functions in Unity as well.
Common Function Types
Here are a few functions that you will see commonly as you script:
Update  Update takes whatever commands are called within it and either applies them on
each frame of animation or checks that they are applicable for each frame.
function Update() {
}
Use Update when you want the script to watch for things to happen as the player plays
(such as do x when the player gets y score).
Start Another common function is Start :
function Start() {
}
Start instructs the computer to run the commands it contains before any Update com-
mands in the same script are run for the first time. Use Start if the object is supposed to
do or load something before anything else happens.
Awake    he Awake function works similarly to the Start function in that it operates once
before other things happen in the script:
function Awake() {
}
However, the Awake function works when the script is being loaded into the scene—essen-
tially, when the object it is a part of becomes active within the game.
LateUpdate  LateUpdate works like Update in that it is called on each frame:
function LateUpdate() {
}
Search WWH ::




Custom Search