Editor Window/Scriptable object - null problem

I have the database that is basically a scriptable object (CellsDatabase):

    [System.Serializable]
    public class CellClassModification
    {
    	public Type name;
    	public int price;
    }
    
    [System.Serializable]
    public class CellClassData
    {
    	public Type name;
    	public GameObject prefab;
    	public List<CellClassModification> modifications;
    	
    	public CellClassData()
    	{
    		name = null;
    		prefab = null;
    		modifications = new List<CellClassModification>();
    	}
    
    	public CellClassData(Type type)
    	{
    		name = type;
    		prefab = null;
    		modifications = new List<CellClassModification>();
    	}
    }
    
    [System.Serializable]
    public class CellsDatabase : ScriptableObject
    {
    	public List<CellClassData> cells;
    
    	public CellsDatabase()
    	{
    		cells = new List<CellClassData>();
    		SyncClasses();
    	}
    
    		. . .
    }

And the editor window for this database

alt text

Each class has a list of upgrades (CellClassModification) that instance of this class can transform into, paying the price.

Every time you open editor window it loads data from the same scriptable object.
And it works good. But after Unity recompiles scripts or Unity restarts I’m getting null reference exceptions here:

activeCellClass.modifications[a].**name**.Equals(...)

where:

  • modification is an instance of CellClassModification,
  • modifications[a].name == null (But it shouldn’t)

Editor Window script

I don’t understand how does modification.name get null value. Maybe some serialization issues?

As @VesuvianPrime said, System.Type is not serializable, you need to use a wrapper that has the type qualified asm name (not just name) serialized inside, and reconstruct the type when the internal value becomes null (when an assembly reload happens). This is how I used to do it in the pre-VFW days :stuck_out_tongue:

[Serializable]
public class SerializedType
{
	[SerializeField]
	private string asmQualifiedName;
	private Type value;
	
	public Type Value
	{
		get { return value ?? value = string.IsNullOrEmpty(asmQualifiedName) ? null : Type.GetType(asmQualifiedName));
		set
		{
			this.value = value;
			asmQualifiedName = value == null ? string.Empty : value.AssemblyQualifiedName;
		}
	}
	
	public SerializedType(string asmQualifiedName)
	{
		this.asmQualifiedName = asmQualifiedName;
	}
	
	public SerializedType(Type type)
	{
		Value = type;
	}
	
	public SerializedType()
	{
	}
}

In VFW, System.Type serialization (and exposure) is directly supported, no need for wrappers 'n stuff.

I did it the way @VesuvianPrime said and it worked well.

So I replaced Type field in data classes by string field and when I nead an actual type I use Type.GetType(“className”).

It may not be the most effiecent way of doing things, but it works.