I'm trying to add a sort of manager to handle all objects in a list and increase a float value that each individual member of the list has given a condition every single frame. I'm having some trouble wrapping my head around the actual code I need though.
So say I've got an OverlapCircleAll or trigger checking for tagged objects in a certain radius. These objects are then added to a List (foo).
Then, I go through foo with a for loop and declare a float value x for each object.
What I need is for each object's x to increase when the object is also on another list, bar.
So something like this:
for (int i = 0; i < foo.Count; i++)
{
float x;
//or float x = 0;?
if (bar.Contains(foo[i]))
{
x += 1f;
}
}
The problem is, I can't figure out how to make the value of x persist each loop. This function is called in Update() so the idea is that the longer an object is in both foo and bar, the higher its value of x is. I don't know how to save the value of x for the next loop though. Ideally something like:
for (int i = 0; i < foo.Count; i++)
{
float x;
if (lastX != null)
{
x = lastX;
}
else
{
x = 0;
}
if (bar.Contains(foo[i]))
{
x += 1f;
}
float lastX = x;
}
But I don't know a) how to save lastX for each object in the list and b) set x to lastX (because for each loop it creates a lastX).
I thought about storing each x in an array of floats and calling the corresponding array[i] to foo[i] to get x, but if I remove something from the list then the array will become desynchronised with the list and all the x values will get muddled up.
What is the correct way to go about this?