Home > Blockchain >  I get an argument out of range exception when i remove the last element of a list, even when I make
I get an argument out of range exception when i remove the last element of a list, even when I make

Time:07-29

I've been trying to change a button prefab's color untill another button prefab is clicked on, but if I select the last element, then delete it, I get an Argument Out Of Range Exception.

I tried to fix it by checking if the previousID is not bigger than the prefabList.Count. But it doesn't work.

Am I missing something?

the code:

public void SetColor(List<GameObject> prefabList, int ID)
{
    if (prefabList.Count != 0)
    {
        if (previousID != ID && prefabList.Count >= previousID)
        {
            Debug.Log("ID: "   ID   "previous ID: "   previousID);
            prefabList[previousID].GetComponent<Image>().color = Color.white;
            previousID = ID;
        }
        else if (ID == previousID)
        {
            prefabList[ID].GetComponent<Image>().color = Color.yellow;
        }            
    }        
}

the rest of the code:

private void Update()
{
    JLList = MenuManager.GetComponent<AddJL>().JLList;

    CheckForChange();
    SetEditorJL.SetColor(prefabList, JLID);
}

private void GenerateOptions()
{
    foreach (Transform child in ScrollViewContent.transform)
    {
        Destroy(child.gameObject);
        prefabList.Clear();
    }

    for (int i = 0; i < JLList.Count; i  )
    {
        GameObject newJLOption = Instantiate(ButtonPrefab, PrefabParent.transform);
        int JLIndex = i;
        newJLOption.GetComponent<Button>().onClick.AddListener(() => LoadJL(JLIndex));
        prefabList.Add(newJLOption);
    }
}

private void LoadJL(int JLIndex)
{
    JLID = JLIndex;
    SetEditorJL.SetColor(prefabList, JLID);
}

private void CheckForChange()
{
    int allJLOptions = ScrollViewContent.transform.childCount;
    if (allJLOptions != JLList.Count)
    {
        GenerateOptions();
    }
}

Edit based on answers:

public void SetUnderEditJudgeLineColor(List<GameObject> prefabList, int ID)
{
    if (prefabList.Count != 0)
    {
        if (previousID != ID && prefabList.Count > previousID)
        {
            Debug.Log("ID: "   ID   "previous ID: "   previousID);
            prefabList[previousID].GetComponent<Image>().color = Color.white;
            previousID = ID;
        }
        else if (ID == previousID)
        {
            prefabList[ID].GetComponent<Image>().color = Color.yellow;
        }
    }        
}

Sadly, I still get the same error.

Edit 2:

    public void SetUnderEditJudgeLineColor(List<GameObject> prefabList, int ID)
{
    if (prefabList.Count != 0)
    {
        if (previousID != ID && prefabList.Count > previousID)
        {
            Debug.Log("ID: "   ID   "previous ID: "   previousID);
            prefabList[previousID].GetComponent<Image>().color = Color.white;
            previousID = ID;
        }
        else if (ID == previousID && ID < prefabList.Count)
        {
            prefabList[ID].GetComponent<Image>().color = Color.yellow;
        }
    }        
}

it doesn't give an argument out of range exception now, but neither ID or previousID gets their values updated anymore.

CodePudding user response:

prefabList.Count >= previousID should be prefabList.Count > previousID. The last index is always List.Count - 1 (because indexes start at 0) so, if the index and count are equal, you're checking past the end of the list.

EDIT:

Your else if statement isn't protecting from prefabList.Count so that'll also need a check against the size of the list:

else if (ID == previousID && ID < prefabList.Count)

  • Related