Reputation: 59
I know that are a lot of similar questions around there, yet I failed to find the solution to my problem.
I have this simple class:
using System;
[Serializable]
public class CountryInternalData
{
public string title;
public string textID;
public int confederationID;
public int numericID;
}
And I'm trying to use an array list of it on another class:
using System.Collections.Generic;
using UnityEngine;
using System;
[Serializable]
public class ConfederationAffiliatedCountriesLink : MonoBehaviour
{
public List<CountryInternalData>[] affiliatedCountriesByConfederation;
//SOME OTHER CODE (unrelated to the question IMO)
void TestAffiliatedCountries()
{
var confederations = contentManager.loadedConfederations;
var arraySize = confederations.Count;
affiliatedCountriesByConfederation = new List<CountryInternalData>[arraySize];
//LOOPS that fill the array list (unrelated to the question IMO)
}
}
Despite using the [Serializable]
attribute, the affiliatedCountriesByConfederation
doesn't appear in the inspector:
I'm under the impression that this might be something basic, but I'm facing a hard time to figure out what I'm missing here. My goal is to build a simple save and load system to create data to my game, so I need to check if the things that I saved are being loaded properly, that's why see the data in the inspector would be very useful for me...
Upvotes: 0
Views: 448
Reputation: 90659
Nested arrays/lists are not serialized. See Script Serialization
Note: Unity does not support serialization of multilevel types (multidimensional arrays, jagged arrays, and nested container types). If you want to serialize these, you have two options: wrap the nested type in a class or struct, or use serialization callbacks ISerializationCallbackReceiver to perform custom serialization. For more information, see documentation on Custom Serialization.
So you could use a wrapper type like
[Serializable]
public class CountryInternalData
{
public string title;
public string textID;
public int confederationID;
public int numericID;
}
[Serializable]
public class CountryInternalDataCollection
{
public List<CountryInternalData> data = new List<CountryInternalData>();
// Indexer for still accessing elements directly via index
// => without the need for writing .data everytime
public CountryInternalData this[int index]
{
get => data[index];
set => data[index] = value;
}
public void Add(CountryInternalData item)
{
data.Add(item);
}
// And evtl other methods if you need them
}
and then use
public CountryInternalDataCollection[] affiliatedCountriesByConfederation;
Upvotes: 2