Using an array of GameObjects to define a variable.

I have a list of targets, that when their health float reaches zero, deletes itself and sends an event to which my “Gun” script subscribes.

public void Damage(float damage)
{
health -= damage;
if (health <= 0)
{
OnEnemyKilled?.Invoke(this, EventArgs.Empty);

source.PlayOneShot(kill, 0.25f);

Destroy(gameObject);
}
}
private void Target_OnEnemyKilled(object sender, EventArgs e)
{
AmmoRegen();
}
public void AmmoRegen()
{
if (this.gameObject.activeSelf)
{
gunData.currentAmmo = gunData.currentAmmo + gunData.magSize / 2;
gunData.currentAmmo = gunData.currentAmmo + 1;
if (gunData.currentAmmo > gunData.magSize)
gunData.currentAmmo = gunData.magSize + 1;
}
}

The GameObject deletion works fine but where I’m running into issues is getting to event to trigger on all objects with the “Target” script attached. Currently, the event will only trigger on a weapon with the “Gun” script attached if it destroys the one GameObject set as the “Target” within the “Gun” script.

8484251--1128257--upload_2022-10-2_13-51-15.png

8484251--1128251--upload_2022-10-2_13-49-15.png

I’ve used an array to grab all objects with a certain tag so that they become “Targetable”, but my question is how do I make the actual target variable use the array of “Targetable” GameObjects as the input, so that my weapon will receive the event when destroying all GameObjects in the array instead of on the one set as “Target”.

Essentially, how do I get this
8484251--1128251--upload_2022-10-2_13-49-15.png
to use this
8484251--1128260--upload_2022-10-2_13-52-49.png
as the “target” input.

When posting chunks of code, you should use the first option, not the second one. Makes it way easier to read.
8484302--1128266--upload_2022-10-2_16-39-50.png
After reading the problem, I’m still having trouble figuring out what you actually want.

I don’t really understand what this means. Are you trying to pass the Targetable array as a parameter for a method inside of Target? It might help using actual game examples instead of describing it.

I understand what you want, but that’s a very roundabout question indeed.
Do you know how lists and arrays work in general?

What stops you from promoting Target target to Target[ ] target and simply passing the result as an array?

Because Target target is a reference to my target script and when I try to make it Target[ ] target it makes it so the event subscription doesn’t contain a definition, like this:
8487227--1128938--upload_2022-10-3_16-33-46.png
8487227--1128932--upload_2022-10-3_16-32-48.png8487227--1128935--upload_2022-10-3_16-33-12.png

assuming that’s what you meant. I haven’t had to use arrays outside of basic integers so I’m obviously not the most experienced with them.

8487227--1128938--upload_2022-10-3_16-33-46.png

You’re supposed to loop through the array and apply your code to each Target individually. You can use a for loop or a foreach loop, those are the most common ways.