- Home /
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
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)
I don't understand how does modification.name get null value. Maybe some serialization issues?
Type isn't serializable. You can look at converting it to/from string.
Answer by vexe · Jan 09, 2015 at 08:02 PM
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 :p
[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.
thanks for the help, I may use this when it comes to optimization
Answer by spaceduck_corp · Jan 10, 2015 at 09:00 AM
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.
Vexe's solution isn't more "optimized", it's more reliable. By simply converting the Type to/from string you're neglecting cases where two Types might have the same name.
Your answer
Follow this Question
Related Questions
ScriptableObject reference resetted after a method is finished. 2 Answers
ApplyModifiedProperties returns false 0 Answers
Why is play mode reverting my scriptableobject to a previous serialized state? 2 Answers
OnInteractivePreviewGUI multiple selection. Works but spams errors. 0 Answers
How to work with custom class (ScriptableObject) SerializedProperty? 1 Answer