Game Development Reference
In-Depth Information
Listing 4.24 Using a GUI.Label to Print the Score to the Screen
var duckCount = 0;
var myStyle: GUIStyle;
function OnGUI () {
GUI.BeginGroup (Rect (Screen.width / 2 − 100, 10,
250, 70));
GUI.Box (Rect (0,0,250,70), "Duck Herding");
GUI.Label (Rect (10,10,250,70),"Score: " +
duckCount + " Time: " +
Time.fixedTime, myStyle);
GUI.EndGroup ();
}
function OnTriggerEnter(collision: Collider)
{
...
}
Step 20. Save and play. Note how the score and time value changes
in size. This is because as the time changes, so do the numbers being
displayed. Because the font being used is not a fixed width, the entire
text length can grow and shrink depending on what is written. To fix this,
instead of putting the score in the middle, set the alignment to MiddleLeft
in the Inspector view of myStyle.
Step 21. Play. You may want to play around with the positioning of the
GUI later, but for now try out some duck herding.
Step 22. Finally, to make the game run out after 2 minutes, we will add a
timer. Edit the finish.js script to reflect the changes in Listing 4.25 .
Listing 4.25 Adding a Game Over Event Timer
var duckCount = 0;
var myStyle: GUIStyle;
var levelLength = 120; //seconds
private var gameOver = false;
function OnGUI ()
{
GUI.BeginGroup (Rect (Screen.width / 2 − 100, 10,
250, 70));
if(gameOver)
{
Search Nedrilad ::




Custom Search