Identify Variable from Triggering Object

Hi! So I’m writing a script on an enemy to detect a hit from a damaging ability and I want to be able to decide how much damage the ability will cause with a float on a script attached to the ability. I want to make this more accessible and changeable for future damaging abilities I add. So, on the damaging object, is a script named damageAmount which contains nothing important except the two floats:

public float Damage;
public float DPS;

I want to be able to use this script on all of my future damaging abilities, adjusting the numbers for each in the future.

Now, the script on the enemy is as follows:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class DummyHealth : MonoBehaviour
{

public float Health;
public float DummyDamagePerSecond;
public float DummyDamageTaken;


// Update is called once per frame
void Update()
{
   /// DummyDamageTaken = col.gameObject.GetComponent<damageAmount>().Damage;   <- I know this is incorrect
}


void OnTriggerStay2D(Collider2D col)
    {
        if (col.gameObject.tag == "DamagingEnemies")
        {
            artCoroutine(InflictDamage());
        }
    }

IEnumerator InflictDamage()
{
    Debug.Log("DamageInflicting...");

    Health -= DummyDamageTaken;

    Debug.Log("Damage Inflicted");

    yield return new WaitForSeconds(DummyDamagePerSecond);
}
}

The part I’m having trouble with is

/// DummyDamageTaken = col.gameObject.GetComponent<damageAmount>().Damage;

My goal is to assign the variable ‘DummyDamageTaken’ to the number that is attached to the Damage variable on the script on the colliding object.

Again, I would like to have the same script on multiple different objects, and then the enemy will identify which object is hitting so it can pull the number from that specific script.

I’m sorry if this is so confusing and complicated, I’m fairly new to programming and am doing my best to learn. If you need me to clarify, feel free to ask!

Hello. If it were me, I would make the health/damage-dealing components a lot more generic, as each object can have damage dealing and health class and a custom way to trigger the damage dealing. From there you can pass in the damage value as a parameter to the health component of the target, where that value will be subtracted from the current health. In terms of creating custom behavior when taking damage, you could create an event delegate that will be called whenever an entity takes damage, and then add your custom damage-dealing behavior to the delegate through the custom entity script. Overall I would recommend separating the functionality over several classes and making each class as generic as possible so it can be reused between objects.

Hi, thank you so much for the helpful feedback! I’ve never heard of classes or event delegates before, could you point me in the direction of some websites or videos I can use to learn about it?