Skip to content

A simple health script for unity game projects. If you find any issues or have any suggestions feel free to tell me :D

License

MIT, Unknown licenses found

Licenses found

MIT
LICENSE
Unknown
LICENSE.meta
Notifications You must be signed in to change notification settings

Mateo-Jimenez76/Unity-Health-Script

Repository files navigation

Unity-Health-Script

Health Inspector

How To Use

Dealing Damage

To deal damage call the Damage(int amount) function. When damage is taken all events in the OnDamage UnityEvent will be invoked and if health is <= 0 then OnDeath will be invoked aswell.

Important

OnDeath is invoked AFTER OnDamage and not instead of.

Implimentation in Health.cs/Damage(int amount)

Healing

To heal call the Heal(int amount) function. This function will also invoke all events in the OnHealth UnityEvent.

Implimentation in Health.cs/Heal(int amount)

Subscribing / Unsubscribing From Events

If you would like to subscribe to the events through scripts you can call SubscribeTo...(UnityAction action) followed by the name of the corresponding Unity Event. Ex: SubscribeToOnDeath(UnityAction action). To unsubscribe is the same process but instead you call UnsubscribeFrom...(UnityAction action) with the UnityAction you wish to remove from the Event.

Health Displayer

Prerequisites

A Health script is required to be on the same object as the HealthDisplayer. If one is not already on it then one will be created for you due to the effect of [RequireComponent(typeof(Health))]\

Displaying As A Bar

image

Prerequisites

A reference to a Slider is required

Displaying As A Number

image

Prerequisites

A TextMeshProUGUI is required. While displaying as a number there are several formats to chose from which go as follows.

Number Display Options

Type Result
Out Of 50/100
Percentage 50%
Raw Number 50
Custom This splits the max and current health values into two separate TextMeshProUGUI objects allowing for the user to manipulate placement and text styles however they please.

Additional Reading

https://docs.unity3d.com/Packages/com.unity.textmeshpro@4.0/api/TMPro.TextMeshProUGUI.html
https://docs.unity3d.com/6000.2/Documentation/ScriptReference/UIElements.Slider.html
https://docs.unity3d.com/6000.2/Documentation/ScriptReference/Events.UnityAction.html
https://docs.unity3d.com/6000.2/Documentation/ScriptReference/RequireComponent.html

About

A simple health script for unity game projects. If you find any issues or have any suggestions feel free to tell me :D

Topics

Resources

License

MIT, Unknown licenses found

Licenses found

MIT
LICENSE
Unknown
LICENSE.meta

Stars

Watchers

Forks

Contributors 3

  •  
  •  
  •  

Languages