I appreciate learning about the UnityEvent system, but for me, since I want to practice general programming skills as well with this course (which I have!), I just took this as practice using Delegates and Events (and honestly, at least to me, it seems like it’s more straight-forward this way lol)
So in Health, I made:
public delegate void OnDamageTakenEvent(float damageAmount);
public OnDamageTakenEvent onDamageTaken;
Then within the Damage function, I did
public void TakeDamage(float damage, GameObject instigator)
{
//Other stuff
onDamageTaken(damage);
}
Then, this part was a little tricky at first, but since the TextSpawner is a CHILD of the actual game object that has the Health component (Player, Enemy) you actually have to use this to subscribe to the event:
if(GetComponentInParent<Health>())
{
GetComponentInParent<Health>().onDamageTaken += Spawn;
}
Now that we’re subscribed, and the delegate-call (is that the proper term?) in Health.TakeDamage function is passing the value of damage, we’re automatically getting sent the data directly from Health to DamageTextSpawner.Spawn. So, you can just take the damage and then set the amount to display on the Text Component.
private void Spawn(float damageAmount)
{
//instantiation of the prefab
damageText.SetDamageAmount(damageAmount);
Destroy(damageText.gameObject, 1f); //Using this instead of a separate script
}
Ultimately it’s all doing the same thing, but I enjoy getting to use mode “code-y” ways of doing things when I can, unless it’s something a designer would want to easily access, in which case that’s when I feel it’s best to expose a thing in the editor.
Anyways! Just wanted to share, for those who may be interested.