Game Development Reference
In-Depth Information
We can have coins with different amounts, by setting the value of
coinValue
. Just like
Collectable
,
Start()
and
Update()
functions are empty. The new function we add to this
script is
Collected
, which takes a value of type
Collector
. This owner is in fact the collector
who is trying to collect this coin. Back to
Collectable
script (Listing 26 in page 76), recall
that
Collectable
sends a message to other scripts to inform them about a collision with a
collector. That message is called
Collected
and includes an attachment, which is the collect-
or. By declaring the method called
Collected()
and giving it the parameter
owner
, we spe-
cify
Coin
script as a receiver of this message. Consequently, when
Collected
message is re-
ceived,
Collected()
function is executed. What does this function do is to get the inventory
box of the collector (
owner
). By calling
owner.GetComponent<InventoryBox>()
, we try to
get a reference to the inventory box, and store this reference in
box
. If no inventory box
found, the value of
box
becomes
null
, and hence nothing is done. However, if the inventory
box exists, the amount of money inside that box is increased by
coinValue.
Finally, the coin
game object is destroyed.
Illustration
36
illustrates
the
interactions
between
Collector
,
Collectable
,
Coin
,
and
CategotyBox
, along with all interactions among these scripts.