So in this section we were going to make our Health Bar reflect the actual health of the enemies and I went on and made a mini challenge to finish it beforehand, now when I see Sam’s solution I am so confused as to why we are using update() to update the Health Bar when we have a very distinct and clearly defined moment in time when the Health Bar needs to be updated from when we created the TakeDamageEvent (unity event in Health.cs)?
My solution:
namespace Scripts.Attributes
{
public class HealthBar : MonoBehaviour
{
[SerializeField] GameObject healthBarImage = null;
Vector3 _scalingVector;
bool _healthBarEnabled = false;
void Awake()
{
_scalingVector = new Vector3(1, 1, 1);
healthBarImage.transform.localScale = _scalingVector;
transform.GetChild(0).gameObject.SetActive(false);
}
public void UpdateHealthBar(GameObject character)
{
if (!_healthBarEnabled)
{
transform.GetChild(0).gameObject.SetActive(true);
_healthBarEnabled = true;
}
_scalingVector = new Vector3(character.GetComponent<Health>().GetHealthPercentage(), 1, 1);
healthBarImage.transform.localScale = _scalingVector;
if (_scalingVector.x <= 0)
{
DeactivateHealthBar();
}
}
void DeactivateHealthBar()
{
transform.GetChild(0).gameObject.SetActive(false);
_healthBarEnabled = false;
}
}
}
HealthBar.UpdateHealthBar() is called in Health.ApplyDamage() by the UnityEvent:
public void ApplyDamage(GameObject instigator, float damage)
{
_health.value = Mathf.Max(_health.value - damage, 0);
takeDamage.Invoke(damage);
if (_health.value == 0 && _isAlive)
{
GainExperience(instigator);
TimeToDie();
}
}
And it is set up on my enemy prefab as such:
And Sam did:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace RPG.Attributes
{
public class HealthBar : MonoBehaviour
{
[SerializeField] Health healthComponent = null;
[SerializeField] RectTransform foreground = null;
void Update()
{
foreground.localScale = new Vector3(healthComponent.GetFraction(), 1, 1);
}
}
}
That is obviously much cleaner and easier to read even though he will probably add a way to deactivate the healthbar when enemies are dead though. But yeah, why had so many Updates running on all the enemies in a scene when we spent time setting up the Unity event previously? And tbh, even if we had NOT set that up I would still prefer to call a updatehealth() method from Health.ApplyDamage() since we do not need to update health elements constantly.
So am I wrong (I suspect I am) and if so, why? Can I keep my way or will it cause issues down the line in the course?