arrayslistunity-game-enginescriptable-object

Unity sciprtableobject containing just a list of 2 element arrays


I'm attempting to put a list of 2 element arrays into a scriptable object. I've finally managed to avoid all compiler issues, but I'm not getting anything to populate in the inspector. I'm thinking I should just throw the SO out the window and create an 2D array. here's the entire class:

[SerializeField]
public static List<string[,]> list = new List<string[,]>();

Goal:

  1. string, string
  2. string, string
  3. and on...

It's been a while since I've played with unity and I'm just trying to make a simple data container. Am I missing something simple?


Solution

  • It's not showing up in the inspector for two reasons:

    1. You cannot see static values in the inspector.
    2. Unity does not serialize lists of arrays.

    If you need to have a list of two strings visible in the inspector, I would suggest creating a new object to define what it is you are listing.

    For example, if you wanted to list items with a name and a description then you could do something like this:

    using System;
    using System.Collections.Generic;
    using UnityEngine;
    
    [CreateAssetMenu(fileName = "New Test SO", menuName = "Test SO")]
    public class TestSO : ScriptableObject
    {
        public List<Item> list = new List<Item>();
    }
    
    [Serializable]
    public class Item
    {
        public string Name;
        public string Description;
    }
    

    Notice the [Serializable] attribute from the System namespace above the Item class. This allows your custom classes to show up in the inspector as well to be serialized elsewhere.

    It looks like this in the inspector with a nice little handle to add and remove items: Inspector for test scriptable object

    I hope this clears things up!