About Nested List

Hi everyone!

public class GameManager : MonoBehaviour
{
    public List<List<GameObject>> m_SlotList = new List<List<GameObject>>
    {
        new List<GameObject>(),
        new List<GameObject>(),
        new List<GameObject>(),
        new List<GameObject>(),
        new List<GameObject>(),
    };
   
    void setList()
    {
        for(int i = 0; i < 5; i++)
        {
            for(int j = 0; j < 10; j++)
            {
                m_SlotList[i].Add(new GameObject());
            }
        }
    }

    void Start(){
         setList();
    }
   
    void Update(){
         Debug.log(m_SlotList[0][0]);
    }

}

When this program starts running, it returns “new GameObject” as log. But when I type m_SlotList [0] [1] in
c # while running Unity, it returns an “ArgumentOutOfRangeException” error. What is the reason of this?

If there is no solution to this, how can I edit the nested list in Inspector?

thanks in advance!

start is not Start
probably

It works, when I make a change in the code while the game is running and save it, it gives the above error. I wonder what caused this problem.

What I want to do is make a nested listee that works like this: ‘List [ListIndex] [ElementIndex]’.

Also “List<List > …” does not appear in Unity Inspector.

You missed my hint

“start” is not “Start”

IF you’re using Visual Studio, it should be indicating this problem to you:

oh, sorry…

You’re right, but I’m using Start (). I just spelled it wrong.

Unfortunately this isn’t the problem =(

yes… it must not be spelled wrong

Because this is not about code, this is about hot reloading. You hadn’t made that clear in your original post that you talk about hot reloading. When you change code while the game is running, all of your code needs to be recompiled. When this happens everything on the managed side is destroyed since the whole C# appdomain is destroyed and reloaded.

When this happens Unity will serialize all objects currently loaded, destroyed the appdomain, compiles the new code and restores the previously serialized objects. However that means only things that can be serialized will survive the assembly reload. Since Lists of Lists are not serializable by the Unity serializer your list will be lost after the reload because Start won’t run a second time.

I recommend reading through the script serialization documentation, especially the hot reloading section and what kind of data can / can not be serialized.

Thank you for your advice and comment!