Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Unity Game Development Blueprints

You're reading from   Unity Game Development Blueprints Explore the various enticing features of Unity and learn how to develop awesome games

Arrow left icon
Product type Paperback
Published in Nov 2014
Publisher
ISBN-13 9781783553655
Length 318 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
John P. Doran John P. Doran
Author Profile Icon John P. Doran
John P. Doran
Arrow right icon
View More author details
Toc

Table of Contents (11) Chapters Close

Preface 1. 2D Twin-stick Shooter 2. Creating GUIs FREE CHAPTER 3. Side-scrolling Platformer 4. First Person Shooter Part 1 – Creating Exterior Environments 5. First Person Shooter Part 2 – Creating Interior Environments 6. First Person Shooter Part 3 – Implementing Gameplay and AI 7. Creating Save Files in Unity 8. Finishing Touches 9. Creating GUIs Part 2 – Unity's New GUI System Index

Adding in points, score, and wave numbers

One of the most important things to do in a game is reward the player and give them a sense of progression. For now, let's reward the player with a score we will display for them and also let the player know exactly which wave he is on. Perform the following steps:

  1. Create a new GUI Text object by going to the Hierarchy tab and selecting Create | GUI Text.

    Note

    If you are using the Unity 4.6 beta or higher versions, instead of creating the object in this way, you will need to select GameObject | Create Empty to create an empty object. Once you've done that with the object selected, then click on Component | Rendering | GUIText to add the component there.

  2. After this, switch to the Game tab, as you will be unable to see GUI Text in the Scene view. Note that the game should still not be started as yet, so don't hit the Play button. Have a look at the following screenshot:
    Adding in points, score, and wave numbers
  3. Change the GUI Text object's name to Score Counter. Under the GUI Text component, change the Text to Score: 0. After that, change the Position of Transform to (0, 1, 0) to put it on the left-hand side of the screen.

    GUI elements are placed in viewport space, which means that the space of the GUI is a value from (0,0) in the bottom-left corner to (1,1) in the top-right corner.

  4. Though this is technically fine, it would be nice to have some space so that the text isn't completely off the screen. To give us a bit of padding, let's set the Pixel Offset property to (10, -10) to move it 10 pixels toward the right-hand side and 10 pixels down. Note that Pixel Offset uses pixel space. Have a look at the following screenshot:
    Adding in points, score, and wave numbers
  5. Now that we have the text set up, let's set the font. Drag-and-drop the Font folder into your project. Then set Font to OSP-DIN and Font Size to 25.

    Note

    The font used in this project was created by the OSP Foundry. For more information about their stuff check out http://ospublish.constantvzw.org/foundry/.

  6. Next, duplicate the Score Counter object by right-clicking and selecting Duplicate. Set this duplicate's name to Waves Counter, and change its text to Wave: 0.
  7. Set the Waves Counter object's Position to (1, 1, 0). Then set Anchor to upper right and Pixel Offset to (-10, -10). Have a look at the following screenshot:
    Adding in points, score, and wave numbers
  8. Now that we have our text files created, let's now have them function correctly! First, let's go into the GameController class. Inside, we need to create some new variables as follows:
    // The values we'll be printing
    private int score = 0;
    private int waveNumber = 0;
    
    // The actual GUI text
    public GUIText scoreText;
    public GUIText waveText;
  9. Next, we will need to add a function to call whenever our score increases, as follows:
    public void IncreaseScore(int increase)
    {
      score += increase;
      scoreText.text = "Score: " + score;
    }

    The += operator will take the current value and add the right-hand side's value to it.

  10. Then, we'll need to call this function inside our EnemyBehaviour component. After the controller.KilledEnemy() line, add the following line:
    controller.IncreaseScore(10);
  11. Finally, whenever we increase the wave number we need to change its text as well. Back in the GameController class after the if(currentNumberOfEnemies <= 0) line, add the following lines:
    waveNumber++;
    waveText.text = "Wave: " + waveNumber;

    The ++ operator will take the current value of a number and increment it by 1.

  12. Save all the script files, go back to Inspector, and set the Score Text and Wave Text objects to the proper variables. After that, run the game. Have a look at the following screenshot:
    Adding in points, score, and wave numbers

And with that, you can see that everything is working together, killing enemies rewards points, and killing all the enemies in a wave triggers the next wave to start!

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at R$50/month. Cancel anytime