Game Development Reference
In-Depth Information
Notice that the variable type we use to reference a 3D Text object is called
TextMesh
. This
script requires
LivesManager
, so it has also to be attached to the main camera. After adding
it, we need to drag the 3d text object we are going to use as lives count display from the
hierarchy to
display
variable. The initial value of
display.text
is set to
startLives
, which is
the initial number of lives according to
LivesManager
.
display.text
is a string that sets the
displayed text of 3D Text, and, since it is string, we need to convert the integer value of
startLives
to string by calling
ToString()
function as in line 16. After setting the initial text
value, all we have to do is to monitor any changes on the number of lives by hand-
ling
OnLifeLost
and
OnLifeGained
messages. Upon each change, we read the provided
new number of lives and update
display.text
according to it. However, when
OnAllLivesLost
message is received, we display the message “Game Over”.
The last topic in this section is player score. Up to now we have developed a fully functional
game with lives and health. What remains is to evaluate player's performance by a score
value. Since we are talking about a survival game, the best thing to use as score is the num-
ber of seconds the player was able to survive. First of all, we need a 3D Text to display the
score, and we are going to add it to the bottom of the screen. Additionally, we need to write a
script that increments the score every second. Like
LivesManager
, score script needs a per-
manent game object to be attached to, so we will use the main camera again for this pur-
pose. Listing 81 shows
ScoreCounter
script, which counts and displays player's score.