Game Development Reference
In-Depth Information
8.
Save the script.
9. Check the console.
The error message says it can't convert an 'int' [integer] to a 'string' . So you will have to convert
or cast the currentBunCount to a string before sending it off to the GUI Text object.
10.
Change the line to:
bunnyCounter.guiText.text = currentBunCount.ToString(); // change the gui text's text
11.
Save the script, and click Play.
With the zombie bunny count clearly visible, the player is motivated to reduce the count as quickly
as possible. While doable, it is a bit challenging.
If you think back to the firing mechanism, you may remember that you had to constrain the rate of
“reload” so the player couldn't fire a steady stream of potatoes into the scene. It would be nice,
though, to reward the player's marksmanship by increasing the firing rate with, say, every 10 zombie
bunnies that are eradicated. Because the population grows in hops and bounds, you will have to
keep a total for the removed count as well as the current total.
1.
In the ScoreKeeper script, add the following variables:
int killCount = 0; // player hits
int hitsRequired = 10; // hits required for reward
In the UpdateCount function, below the GuiText line, add the following:
2.
// manage player rewards
if(adjustment == -1){ // if it is a removal...
killCount ++; // increment the count
}
For the reward, you will use a little modulus math. The % operator returns the remainder from a division
operation. You will be dividing by the hitsRequired number, 10. When the remainder is 0, providing the
killCount is greater than 0, you will know the player has destroyed another 10 zombie bunnies.
Below the killCount line, add the following:
3.
int remainder = killCount % hitsRequired; // do the calculation
print("remainder = " + remainder + ", " + killCount + " dead");
if (remainder == 0 && killCount >0){
print("reward time!");
}
4.
Save the script.
5.
Click Play, and watch the console as you destroy the zombie bunnies.
With the mechanism in place, you can add the reward. The reward will be a rate increase in the firing
code, but the number will have a ceiling, or floor in this case.
 
Search WWH ::




Custom Search