O'Reilly logo

Mastering Unity 2017 Game Development with C# - Second Edition by Alan Thorn

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Comments

  • The fValue private float variable stores the health value itself. It's declared with two C# attributes: SerializedField (to show the value in the Inspector) and Range, to display as a slider.
  • The fValue variable is accessed through the Get and Set methods for a C# property. The Get method simply returns fValue. The set method updates the value and validates it.
  • The Set method invokes the HealthChanged event, since the health has changed, and if the health equals or falls below zero, the HealthExpired event is invoked, allowing any visually scripted events in the object Inspector to run.

With the Health script coded, you can now attach it to any object that should have a health property, including the player and all of his/her enemies. ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required