Comments

  • The PlayerHealth variable references the Health component on the player object. This variable is assigned during the Awake function by searching the scene for an object tagged as Player. Once found, the health component is retrieved. You can display health for other characters (such as zombies), however, by exposing the PlayerHealth field as public and manually assigning a reference to a different object.
  • The UpdateHealth function updates the Width of the health bar based on the health of the player. The RectTransform.sizeDelta variable controls the width of the health bar.
The UpdateHealth function can be called on every frame (such as in Update) to update the GUI health status. However, continually using update in this way is ...

Get Mastering Unity 2017 Game Development with C# - Second Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.