List<> Size Changes Without Removing Elements

Extremely weird issue. I declare a List of ints and add to it. Later on, after one of the values in the list becomes 0, that specific item is removed.

However, I’ve noticed that the value for the list size changes seemingly arbitrarily. See code:

public void InitializeGrid(List<float> ratios)
    {
        myRand = new SysRand();
        // This method creates the grid after the user inputs the necessary initial and transition information

        // make sure ratios is as big as our numStates
        while (ratios.Count < numStates)
            ratios.Add(0);
        while (ratios.Count > numStates)
            ratios.Remove(ratios.Last());

        //Get a list of how many cells of each type we need
        List<int> agentCount = new List<int>();
        //int totalCells = gridWidth * gridHeight;
        int totalAgents = 0;
        for (int i = 0; i < numStates; ++i)
        {
            agentCount.Add((int)ratios*);*

totalAgents += (int)ratios*;*
Debug.Log(“Hi!”);
Debug.Log(agents.Count);
}

// this is a list of which states still needed to be added
List stateIndex = new List();
for (int i = 0; i < numStates; ++i)
stateIndex.Add(i);

float[] agentAmountPerState = GetAgentAmountPerState(ratios);

for (int i = 0; i < gridWidth; ++i)
{
for (int j = 0; j < gridHeight; ++j)
{
// Get a random number for the states left to do and set it to the grid
// we can’t just use the number directly because stateIndex
// could be 0,3,4 if we’ve finished setting all the 1s and 2s
//Debug.Log(i + ", " + j);
int index = GetIndexFromRange(agentAmountPerState);
int state = stateIndex[index];
grid[i, j] = new BlankGrid();
grid[i, j].AddAgent();
grid[i, j].agent.currentState = state;
grid[i, j].containsAgent = true;
//uygugykhkjhb;//THIS NEEDS double-checked. Should only fire (I think) if the grid location contains an agent.

// Subtract that state from its agentcount
// And if it goes to zero remove it from both our lists
Debug.Log(agentCount.Count);
for (int g = 0; g < agentCount.Count; g++)
{
Debug.Log(agentCount[g]);
Debug.Log(agentCount.Count);
}
agentCount[index]–;
if (agentCount[index] == 0)
{
Debug.Log(index);
Debug.Log(agents.Count);
agents.RemoveAt(index);
stateIndex.RemoveAt(index);
ratios.RemoveAt(index);
agentAmountPerState = GetAgentAmountPerState(ratios);

}
}
}
}
I’m not sure how to write the console output to a file, but when I run the program and choose 2 “numStates,” I get (for line 20 or so):
Hi!
0
Hi!
0
So it’s telling me that the count is zero, even though it’s CLEARLY counting off the appropriate number of times.
Next, for the for loop and the Debug.Log before it (line 49), I DO get the correct number–2 in my case.
Then, after the index falls to 0 and agentCount is checked (line 59), it is magically 0, before the removal of the list item is even attempted (and the attempted removal throws an argument out of range exception).
I don’t have the slightest clue what I’m doing wrong . Can anyone help me?

This is very hard to follow for me. But I can say that you are at first adding to agentCount list and then printing agent.Count which you havent added to yet.