Game Development Reference
In-Depth Information
when this text is moved next to the BulletGUI text, it will create a result like that
shown in Figure 10.19.
8. Save your Unity scene and project.
While scripting the behaviors of the zombie and gun were complex, objects
like GUI Text are simple to script. Next, you will create a score counter.
Figure 10.19
The ammo counter
Adding a Score Counter
Game actions mean more when there is some sort of feedback attached to them. While
muzzle flashes on the gun and zombie ragdolls are great, these special effects technically
have no nonvisual value to the player. That is why you will now add a score counter.
1. If it is not already open, open your Unity project and scene.
2. Create a GUI Text object named ScoreDynamic and give it a font size of 48 . Place it in
the upper-right corner of your screen with the placement options in the Inspector
menu.
3. Create a new JavaScript file called ScoreDynamic.js in the Scripts folder and type the
following into it:
static var score : int = 0;
function Start () {
score = 0;
}
function Update () {
guiText.text = “Score: “+score;
}
function NewScore (PointValue : float) {
score += PointValue;
}
This script changes the text of the GUI Text object to statically show “Score:” and
display an integer variable. The NewScore function responds to a variable sent to it
called PointValue and adds it to the score integer. You may notice that Score is a static
variable. If you were to create a game-over or high-score screen that showed players
their score after a session, you could access this variable from those other scenes.
4. If you commented out the lines of code in the zombie prefab's BodyDestroy.js script
that send PointValue to this score script, uncomment them now. Now when you kill
a zombie you will get 100 points added to your score (Figure 10.20). Save your Unity
scene and project.
Now players can get some feedback as they play the game. In the next section, you will
give the player some much-needed aid by adding health and ammo pickups.
Search WWH ::




Custom Search