Time for action – keeping score
A short script for keeping track of the player's score will constitute the focus of this short section.
To make this tank work, we are going to need three scripts. The first is fairly simple. Create a new script and name it
ScoreCounter
. It will, as the name implies, track the score. Create it in theScripts
folder and clear out the default functions, just like every other script we have made so far.Add the following line of code to the new script:
public static int score = 0;
For the most part, this should look familiar from the previous chapter. First we define an integer counter. Because it is static, other scripts (such as the ones we will create for the targets) will be able to modify this number and give us the score.
We follow with an
OnGUI
function that defines aRect
class and displays the score using theGUI.Box
function. A Box is just like a Label, but it has a black background by default. This will make it easier to see as we move around.public void...