I’m using lists to be able to use more than one engine in my SpaceShip game.
So, there is the engine Class that has variable ForceGenerated and FuelCapacity.
How I can be able to use List<>.Sum*(all variable ForceGenerated)*
I don’t know the syntax to use this Sum (I saw docs on Microsoft, but I still don’t understand)
float totalForceGenerated = 0.0f;
float totalFuelCapacity = 0.0f;
foreach(NameOfEngineClass e in nameOfList)
{
totalForceGenerated += e.ForceGenerated;
totalFuelCapacity += e.FuelCapacity;
}
Just loop through your list and add them all in a variable.
public float totalForce;
// you probably already have this list elsewhere I'm just showing it so you see where I get 'engines'
public List<Engine> engines = new List<Engine>();
void AllForce () {
totalForce = 0.0f;
foreach(Engine e in engines) {
totalForce += e.ForceGenerated;
}
}