Making a List of Lists inside of a Scriptable Object

With some help, I was able to create a Scriptable Object with a List holding a class:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Unity.VisualScripting;

[CreateAssetMenu(fileName = "New LevelData", menuName = "Level Data", order = 51)]

public class LevelData : ScriptableObject
{
    [SerializeField]
    public List<AuxNeem> list;

    [System.Serializable]
   
    [IncludeInSettings(true)]
    public class AuxNeem{
        [SerializeField]
        public NeemData superNeem;
       
        [SerializeField]
        public FeemData Feem;
   
    }
}

It works amazingly now, but now I am getting ambitious.

  • My Scriptable Object allows me to easily create a List of AuxNeems.
  • I would like to be able to create multiple Lists of AuxNeems within the same Scriptable Object.

Any advice on how to make such a List of Lists inside of this Scriptable Objects?

just use type List<List>

Like this?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Unity.VisualScripting;
[CreateAssetMenu(fileName = "New WordData", menuName = "Word Data", order = 51)]
public class WordData : ScriptableObject
{
    [SerializeField]
    public List<List<AuxNeem2>> list;
    [System.Serializable]
  
    [IncludeInSettings(true)]
    public class AuxNeem2{
        [SerializeField]
        public NeemData superNeem;
      
        [SerializeField]
        public FeemData Feem;
  
    }
}

List<List<>> won’t be serializable. You’ll need to create a container object:

[Serializable]
public class AuxNeemContainer {
  public List<AuxNeem2> auxNeems;
}

[Serializable]
public class AuxNeem2 {
  // SerializeField is unnecessary for public fields
  public NeemData superNeem;
  public FeemData Feem;
}

public List<AuxNeemContainer> list;
5 Likes

Thank you, that worked perfectly!

I am slowly learning how to write Scriptable Objects that include Lists and Lists of Lists, etc. Is there a resource/tutorial on this stuff? Or even just a library of Scriptable Objects that I can read carefully and learn from.

1 Like

ScriptableObjects just use Unity’s serialization system, which is documented fairly well in the manual. If you read up on that it covers all of the fundamentals, and from there it’s just applying them, and occasionally experimenting to make sure things behave as you expect.

Thank you for posting this.
I can not figure out how to use this correctly. It seems logical 100% and is implemented fast BUT how can you check if a Container or subcontainer is already existing?
How can you add a new subcontainer then and how can you delete a list entry? Etc. ?

I’m pretty sure all of these questions can be answered by familiarizing yourself with the C# List API. All of the operations you asked about are handled here:

I have now changed the code to this. Same as above, but using Waves of Creeps instead:

[Serializable]
public class WaveContainer
{
    public List<CreepContainer> creepCont = new List<CreepContainer>();
}

[Serializable]
public class CreepContainer
{
    public int HD_VAl;
    public float Speed_VAL;
}

public List<WaveContainer> myWaveCreepContainer = new List<WaveContainer>();

As you can see I have added the parts:
= new List();
and
= new List();

This was the part missing to make it work correctly.

Anything against doing it this way?

Thank you!