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:
You'll notice that three objects get created at this point: Canvas, Text, and Event System. Right now, we only need to worry about Text; however, the three objects are all needed in order for Unity's new UI system to function properly. We will be discussing this in much more detail in a later chapter.
As you can see, the text is much larger than the player, and there seems to be a white box surrounding the object. This is a representation of what the UI will look like, with the white box being the screen. Rect Transform is currently set up to represent a position of (0,0) to be the center of the world and currently the text is -21 pixels away from the center in the x axis and -97 in the y. If we were to change it to (0,0), you'd notice that the text would now be centered.
Text
object to Score Counter
and then change the Anchors Min Max and Pivot properties to (0, 1); then reset Pos X to 10 and Pos Y to -10 to move them slightly away from the edge of the screen. Lastly, change the Color to white to make it easier to see.OSP-DIN
and Font Size to 25
.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/.
Waves Counter
, and change its text to Wave: 0
.GameController
class. Inside, we will first need to let the script know to use Unity's new UI system:using UnityEngine.UI;
[Header("User Interface")] // The values we'll be printing private int score = 0; private int waveNumber = 0; // The actual GUI text objects public Text scoreText; public Text waveText;
public void IncreaseScore(int increase) { score += increase; scoreText.text = "Score: " + score; }
The +=
operator will take the current value of the variable and add something additional to it.
EnemyBehaviour
component. After the controller.KilledEnemy()
line, add the following line:controller.IncreaseScore(10);
GameController
class after the opening { of the if(currentNumberOfEnemies <= 0)
block, add the following lines:waveNumber++; waveText.text = "Wave: " + waveNumber;
Game Controller
object. After that, save your project and run the game. Have a look at the following screenshot:And with that, you can see that everything is working together, killing enemies will reward the player points, and killing all of the enemies in a wave triggers the next wave to start!
3.145.172.159