Game Development Reference
In-Depth Information
Figure 5-46. The OnTriggerExit event triggering the code that stops the bench from moving
8.
Save the Scene, and save the project.
With a few scripting basics under your belt, most of the rest of the scripting you do will be
for the game.
Summary
In this chapter, you got your first look at scripting in Unity with C#. You found that when you created
a new script, it came partially set up with the class it represented already defined and the two most
common functions blocked in and ready to use. You learned that all Unity scripts derived from a built-in
class named MonoBehaviour and that scripts are where you will control most of the functionality in
your games. Scripts, you discovered, when added to gameObjects, became components.
Your first experiments were with variables, where you learned that besides the regular types (number,
strings, and Booleans), Unity gameObjects and components could also be types. You learned the
rules and Unity conventions for naming scripts, variables, and, later, functions. You discovered that
variables could be hidden or exposed in the Inspector and that values added there replaced any
that were initialized with the variable declarations. With Unity-specific types, you learned to assign
the values; you either had to drag the gameObject onto the parameter in the Inspector or let the
engine “find” the one you wanted in the Start function. Some components, you found, came already
predefined as accessible from your gameObjects, while others, especially any custom scripts, had to
be defined by type before they could be identified for later access.
 
Search WWH ::




Custom Search