Game Development Reference
In-Depth Information
Lines 10 and 13. Two variables have been added: TotalCash representing
the total amount of cash to collect in the level, and cash , a private variable
representing the amount of cash actually collected. The TotalCash variable
could instead be added to the GameManager or to a separate Level class since
it relates specifically to the level , as opposed to the player.
Lines 31-46. The Cash property gets and sets the private cash variable for the
PlayerController. If all cash has been collected, then a CashCollected event is
fired. Typically, the GameManager will listen for and handle this event to end the
current level and move to the next.
After adding this code to the First Person Controllers, be sure to test it by running the game in-editor
and collecting power-ups. Use Debug.Log statements to help determine program flow. Get into the
habit of testing often. Don't simply write many pages of code without testing, even if you're confident
there are no mistakes. It's easy to overlook even typographical errors. Instead, add some lines of code,
test them, and then fix, if required. Back and forth between coding and testing. This helps you to
eliminate bugs before they even get introduced.
For testing cash collection, I'd temporarily turn the Cash variable into public, as opposed to private, so
I can see its value in the Object Inspector. Then I'd play-test in-editor, observing the Cash value in the
Object Inspector as power-ups are collected. Remember, the Object Inspector is a powerful debugging
tool (see Figure 5-14 ) as well! Don't forget to switch Cash back to private once you're done.
Figure 5-14. Using the Object Inspector as a debugging tool to observe cash collection behavior
Search WWH ::




Custom Search