Game Development Reference
In-Depth Information
The following are the comments on the code sample 3-3:
Line 07 : This class features a public reference variable Handler . Using this
field, you can drag-and-drop any component via the Object Inspector into
the Handler slot. This represents the component to which a message will be
dispatched. Notice that its class type is MonoBehaviour or any class derived
from this. This means type agnosticism is achieved and we don't need to
know the object type in advance.
Line 14 : The Invoke method of MonoBehaviour is called to run any method
of matching name. The second floating point argument specifies the time in
seconds, after which the function should be invoked. A time of 0 specifies
immediate invoking.
More information on the Invoke function can be found in the
online Unity documentation at http://docs.unity3d.com/
ScriptReference/MonoBehaviour.Invoke.html .
GameObjects and the world
Another pivotal task in Unity involves searching for objects in the scene from script,
especially if objects are instantiated at runtime. Tasks such as "get me the player
object" and "get me all enemies in the scene" are important for many operations,
from respawning enemies and power-ups to repositioning the player and checking
collisions between objects. To retrieve references to specific GameObjects , Unity
offers a set of functions associated with the GameObject class. These functions can
be useful but expensive, so be sure to call them during one-off events, such as Start
and Awake , wherever possible. Let's explore these further, in addition to
other techniques and methods to work with found objects.
 
Search WWH ::




Custom Search