Game Development Reference
In-Depth Information
16 //--------------------------------------------------------------
17 void Start()
18 {
19 //Get Player Controller Component
20 GameObject PlayerObject = GameObject.Find("Player");
21 PC = PlayerObject.GetComponentInChildren<PlayerController>();
22 }
23 //--------------------------------------------------------------
24 // Update is called once per frame
25 void Update ()
26 {
27 //Update health and ammo strings
28 AmmoLabel.LabelData.text = "Ammo: " + ((PC.ActiveWeapon.Ammo < 0) ? "None" :
PC.ActiveWeapon.Ammo.ToString());
29 HealthLabel.LabelData.text = "Health: " + Mathf.Clamp(PC.Health,0,100).ToString();
30 }
31 //--------------------------------------------------------------
32 }
Line 29. The Mathf.Clamp utility function is used while constructing an ammo
string for the active weapon. Clamp ensures a numerical argument is always
within a specified minimum and maximum range, rounding the value to the
nearest number at the minimum or maximum extreme, if required.
Add the GUIUpdateStats class to the root GUI object in the scene, and then use the Object Inspector
to specify the two label controls for the text data. To do this, simply drag and drop the existing
GUILabel components in the Objector Inspector into the GUILabel fields of the GUIUpdateStats
component (see Figure 8-15 ).
Figure 8-15. Referencing label widgets in the GUIUpdateStats class, for rendering status text on the screen
 
Search WWH ::




Custom Search