Game Development Reference
In-Depth Information
Chapter 4
Using Scripts and
the GameObject
Now that you have programming basics under your belt and a good grasp on the Unity editor and
accompanying developer tools, it's time to introduce scripting to the game scene.
Scripts work hand-in-hand with the Unity game engine. Your scripts customize game object behaviors
using the classes, functions, and variables of the Unity API. You've already seen when you create a
new project, Unity creates a very basic, functional game that consists of one empty scene. Similarly,
when you create a new script, Unity populates it with an empty Start() and Update() function.
The Unity game engine does a lot of work behind the scenes. It runs the Start() and Update()
functions on every game object whether you attach a script or not. When you add code between the
curly braces of the Start() or Update() functions like you did in Chapter 2, you are overriding the
function.
The Unity game engine will still run the Start() and Update() functions at the appropriate time
during game initiation and play. But where initially they were empty and did nothing, adding your
code has redefined what occurs within the game when each of these functions is called.
It is important to understand a little of this behind-the-scenes action. You need to understand when
the game engine is calling the various functions in order to decide which function is the right one to
override so the game objects behave the way you want them to.
You'll start using some of the programming commands introduced in Chapter 2 to affect the
game object's behavior either directly or with player interaction. You'll also start using classes and
functions from the Unity API that are common to just about every game genre.
I strongly recommend that you take the time to type the sample code directly into the script yourself
rather than cutting and pasting it. It does take a little longer, but that's the point. Typing it in yourself
forces you to slow down and focus on each piece of the code, which in turn helps you remember
and recall the concepts learned here.
85
 
Search WWH ::




Custom Search