Can I "combine" two arrays to do something on them, in one loops instead of two?

Hi, I have a ‘Zombies’ and ‘Bats’ arrays (of type ‘Enemy’), I want to get their total attack damage and inflict damage to the player by that
amount, is it possible to do it in one go (loop)? or in a shorter way that what I’m doing?

Here’s what I’m currently doing

float totalDamage = 0
foreach(Zombie z in Zombies)
	totalDamage += z.AttackDamage;

foreach(Bat b in Bats)
	totalDamage += b.AttackDamage;

Player.InflictDamage(totalDamage);

Thanks :slight_smile:

You could use Linq for that :slight_smile:

using System.Linq;

IEnumerable<float> attackDamages = zombies.Select(z => z.AttackDamage).Concat(bats.Select(b => b.AttackDamage));
float total = attackDamages.Sum();
Player.InflictDamage(total);

http://docs.unity3d.com/Documentation/ScriptReference/Array.Concat.html