Help with List<List<int>> please

Hello guys,

I created a class derived from MonoBehaviour that contains a List missionData.

I added this MonoBehaviour to 2 diferent game objects, the missionData in both gameObject’s class, has 3 integers but they have different values. Of course the name of the List in both instances of the class is the same missionData.

On a different class I have a List<List> listOfMissionData which I want to populate with all the missionData Lists that is in ,say 3 gameObjects.

So the question is: Can the listOfMissionData List have 2 missionData Lists without overwriting each other, and can it know that the two Lists are different although the name is the same?

Regards,
Carlos

public class Xxx : MonoBehaviour
{
    List<int> a =  new List<int>{1,2,3,4,5};
    List<int> b =  new List<int>{6,7,8,9,10};
    List<List<int>> c = new List<List<int>>();
    // Use this for initialization
    void Start () {
        c.Add(a);
        c.Add(b);
        Debug.Log(c[1][2]);
    }
    // Update is called once per frame
    void Update () {
    }
}

It’s totally legit and will print ‘8’ in the console.

c[0][0] will give you the number 1 from the ‘a’ list inside ‘c’, c[0][4] will give you the 5.
c[1][0] gives you 6 from the list ‘b’ inside the list ‘c’.

2 Likes

Hello LurkingNinjaDev,

Thanks for your fast response!

Then perhaps you can see what I might be doing wrong here? Shouldn’t this work?

public class ListOfListClass : MonoBehaviour
{

    List<List<int>> listOfLists = new List<List<int>>();

    public void PopulateThisList(List<int> whatListToAdd)
    {
        if (listOfLists.Count < 1)
        {
            listOfLists.Add(whatListToAdd);
        }
        else
        {
            for (int i = 0; i < listOfLists.Count; i++)
            {
                if (listOfLists[i][0] == whatListToAdd[0])
                {
                    listOfLists[i] = whatListToAdd;
                }
                else
                {
                    listOfLists.Add(whatListToAdd);
                }
            }
        }

        SaveData();//This Method saves below:

    }

    void SaveData()
    {
        ClassThatSavesData otherClass = new ClassThatSavesData();
        //otherClass is somewhere else, this is not important though.
        otherClass.listOfLists = this.listOfLists;
    }

    void LoadData()
    {
        ClassThatSavesData otherClass = new ClassThatSavesData();

        this.listOfLists = otherClass.listOfLists;
    }

}

public class TestScript : MonoBehaviour
{

    public List<int> listOfIntegers = new List<int>() { 0, 0 };
 
}



//I add the TestScript to two different gameObjects and in each one I change the values inside intList<int>

//GameObject One with the TestScript as a Component:
//On Start I change the value of the listOfIntegers as so

void Start()
{
    ListOfListClass aboveClass = GameObjectWithThatClass.GetComponent<ListOfListClass>();
    listOfIntegers.Add(8);
    listOfIntegers.Add(3);
 
    aboveClass.PopulateThisList(listOfIntegers);
}

//I add the TestScript to two different gameObjects and in each one I change the values inside intList<int>

//GameObject One with the TestScript as a Component:
//On Start I change the value of the listOfIntegers as so

void Start()
{
    ListOfListClass aboveClass = GameObjectWithThatClass.GetComponent<ListOfListClass>();
    listOfIntegers.Add(4);
    listOfIntegers.Add(6);

    aboveClass.PopulateThisList(listOfIntegers);
}

When I load using the load method I only get one list(the last I added)!?
I would appreciate any help

Regards,
Carlos

I’m not at my own computer right now so I can’t fire up Unity to debug your code, but it seems on line 18

                   listOfLists[i] = listOfIntegers;

and on line 22

listOfLists.Add(listOfIntegers);

you actually wanted to add the whatListToAdd to the listOfLists?

1 Like

Yes, In line 18, since “listOfLists” already has the “listOfIntegers” in the Method’s parameter, I just want to replace the list “listOfIntegers” to keep the values updated.

On line 22, the “listOfLists” doesn’t contain yet the “listOfIntegers” so I want to add it to the former.

Keep in mind that the call to this “PopulateThisList” can be made from many gameObjects containing the “TestScript” class.

I have a game object manager with the “ListOfListClass”

then the “TestScript” class is a component in many gameObjects in the scene, as they update the values in their own “listOfIntegers”, they fire the “PopulateThisList” Method in the “ListOfListClass”(perhaps I should have called the method "UpdateListsValues or so) but that is what that method does, it updates or add the lists to the “listOfLists” list, on a case by case bases.

If the list in the parameter already exists it updates it by replacing it, if the list doesn’t exists then it adds it to the 'listOfLists" list.

I just can’t find if I am doing something wrong, and if so where

I appreciate very much you are taking from your time to try helping me.

Regards,
Carlos

I have a couple of issues with your function here:

    public void PopulateThisList(List<int> whatListToAdd)
    {
        if (listOfLists.Count < 1)
        {
            listOfLists.Add(whatListToAdd);
        }
        else
        {
            for (int i = 0; i < listOfLists.Count; i++)
            {
                if (listOfLists[i][0] == listOfIntegers[0])
                {
                    listOfLists[i] = listOfIntegers;
                }
                else
                {
                    listOfLists.Add(listOfIntegers);
                }
            }
        }

        SaveData();//This Method saves below:

    }

You use whatListToAdd and listOfIntegers interchangeably. You may not notice anything wrong because you’re also passing in listOfIntegers to this function, but if you were to pass in a different list… you’ve got a bug.

Secondly, you’re adding to listOfLists while iterating through it. Let’s pretend you have 100 lists in it. You want to add a new list to it. You begin looping 100 times. On each iteration, you potentially re-add listOfIntegers to the end. I’m pretty sure that’s not what you wanted!

Hello BlackPete,

Thanks you for your response! Got your point. In the first issue you noticed, it was just a typo, I only use the parameter of the method, I will edit the post to reflect that. Sorry about that(Good catch!).

On the second: There is only one class that can fire this method, that is the class that is in many objects and I mentioned. Now when the Method is fired, there are only two possibilities: 1 the list in the parameter is already there, so it will be found by the iteration. 2 It is not there and it will be added. I have tested that iteration and it doesn’t re-add a list at the end unless the list is not there yet. If the list is there already it will just update it with the
"listOfLists = whatListToAdd".
Regards,
Carlos

Try your function with 3 or more different lists. Add them all. What do you see in listOfLists in the end?

Hello BlackPete,

I have done that, I have 10 gameObject with the TestScript which contain the List listOfInt, the 10 gameObjects have different values in the lists and all of them fire the PopulateThisList Method in the Manager script, and everything works correctly. When I repeat the call to that Method from any of the gameObjects, if the listOfInt already exists in the listOfLists, the Method replaces the list, it doesn’t add it again. So that works fine.

Let me try describing the situation as best as I can:

My scene has a gameobject (manager) with the script containing List<List> listOfLists.

My scene has 10 gameObjects with a script containing List intList.

At the Start Method of the script containing intList which is in the 10 gameObjects, I call the Method PopulateThisList that is in the Manager gameObject’s script.

I debug.Log in that Method and I get back that listOfLists.Count = 10. During a game section this 10 gameObjects call the method several times and the debug.Log still returns that listOfLists.Count = 10.

When I check each listOfLists content, I get the each of the listOfInt for each gameObject with the right values.

Example:

listOfList[0] = listOfInt and listOfInt[0] has the correct value, and so on.

Regards,
Carlos