- Home /
How to target multiple enemies (add to list)
Hi! I'm pretty new to Unity and completely new to the forum so apologies if I do something wrong. I'm trying to target a number of enemies with an ability. The number of targets is defined by a scriptable object so it needs to be able to change depending on the ability. I'm trying to do it by adding the enemies to a list when you click them, but I can't get it to work and can't find anything by Googling (I'm at that stage where I don't quite know what to Google). At the moment it seems to add all the enemies as soon as I activate the ability. (It's turn based combat if that makes any difference).
if (!abilityOne.IsGather)
{
do
{
targetCD = abilityOne.NumTargets;
}
while (targetCD < 1);
if (Input.GetMouseButtonDown(0) && targets.Count < abilityOne.NumTargets)
{
IDamagable target1 = (hit.collider.gameObject.GetComponent<IDamagable>());
targets.Add(target1);
}
if (targets.Count == abilityOne.NumTargets)
{
foreach (IDamagable target in targets)
{
int randdam = Random.Range(abilityOne.DamageL + focus, abilityOne.DamageH + 1);
int randheal = Random.Range(abilityOne.HealL + focus, abilityOne.HealH + 1);
Vector3 targetlocation = target.Location;
if (randdam > abilityOne.DamageH)
{
randdam = abilityOne.DamageH;
}
if (randheal > abilityOne.HealH)
{
randheal = abilityOne.HealH;
}
if (target != null && Vector3.Distance(Location, targetlocation) < abilityOne.AbilityRange)
if(water >= abilityOne.CostWater && actionpoints >= abilityOne.CostAP)
{
if (abilityOne.IsAttack)
{
target.Damage(randdam);
randdam = 0;
}
if (abilityOne.IsHeal)
{
target.Heal(randheal);
}
if (abilityOne.Knockback == Knockback.Target)
{
float force = 500;
Vector3 dir = transform.position - target.Location;
dir = -dir.normalized;
target.Falling = true;
target.obj.GetComponent<Rigidbody>().AddForce(dir*force);
}
}
}
targetCD -= 1;
}
if (targetCD == 0)
{
water -= abilityOne.CostWater;
actionpoints -= abilityOne.CostAP;
ability = 0;
}
}
}
Also if I don't have the do while loop bit it just goes straight to the bottom bit and ends the ability. As you can probably tell I have no real idea what I'm doing. What is it I'm doing wrong? Or is there just a whole better way of doing this that's completely different?
Answer by ddooms · Jan 07, 2021 at 08:59 AM
It does seem like you're making a mountain out of a mole hill. If you use lists, you can basically pass a regex that only accepts the ones with the ability. There's a library you need to import though, I think it's linq? Anyway, you can use this:
List<ability> abilities = new List<ability>();
Abilities = FindObjectsOfType().Where((g) => g.getcomponent<Ability>().type == some type);
My syntax is probably off here, but the overall idea is the same. Look up the .Where keyword for lists.
Your answer
Follow this Question
Related Questions
List within a list problem 1 Answer
In a group of gameobjects, how to have only one object active at a time while disabling the others? 2 Answers
How to modify array values? 1 Answer
Affect every object in array. 1 Answer
How to determine a "cone of influence" used for targeting an object. 2 Answers