I would prefer to have a list outside of your average method. Also, i do not see where your changingFloatValue changes in your example. The for loop shown will push the same value into the list 10 time, average being the same as the value. Consider changing to something like this.
// Max number of values to average
//
private int maxAverageValues = 10;
// Running list of values to average
//
private List<float> valuesToAverage = new List<float>();
private float AddValueGetAverage(float newValue)
{
// New values are added to the end of the list
//
valuesToAverage.Add(newValue);
// Check if we exceeded the max amount of values we want to average
//
if (valuesToAverage.Count > maxAverageValues)
{
// Max exceeded, remove the oldest value
//
valuesToAverage.RemoveAt(0);
}
// Return the average
//
return valuesToAverage.Sum() / valuesToAverage.Count;
}
EDIT
As per Amys suggestion, here is the same code using Queue instead of List.
Per Microsofts documentation:
Objects stored in a Queue are inserted at one end and removed from the other. Queues and stacks are useful when you need temporary storage for information; that is, when you might want to discard an element after retrieving its value. Use Queue if you need to access the information in the same order that it is stored in the collection.
private int maxValuesForFloatingAverage = 10;
private Queue<float> floatingAverageValues = new Queue<float>();
private float changingFloat = 0f;
private float changingFloatAvg = 0f;
void Update()
{
// update the changing float value here...
// Enqueue the new value
//
floatingAverageValues.Enqueue(changingFloat);
// Check if we exceeded the amount of values we want to average
//
if (floatingAverageValues.Count > maxValuesForFloatingAverage)
{
// Max exceeded, dequeue the oldest item
//
floatingAverageValues.Dequeue();
}
// Take the average
//
changingFloatAvg = floatingAverageValues.Sum() / floatingAverageValues.Count;
}