Save list's subclass types?

Hello,
I need some help saving the type of class a list holds. Currently I have a script called QuestEntity which contains a list<> of QuestNodes. These QuestNodes are all of different types (for example: StartNode), but they all inherit from QuestNode. When I exit unity they all get reset to the base QuestNode, and are not recognized as their specific type (StartNode). Does anyone know how to get around this.

Code:

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

[System.Serializable]
public class QuestNode
{
	
	[SerializeField]
	public Rect rect;

	[SerializeField]
	public List<QuestNode> nextNodes = new List<QuestNode>();

	public QuestNode()
	{
		rect = new Rect (0, 0, 140, 70);
	}

	public override bool Equals (object obj)
	{
		if(!(obj is QuestNode))
			return false;
		QuestNode n = (QuestNode)obj;
		return n.ToString () == ToString ();
	}

	public override string ToString()
	{
		return "Q-Node";
	}

	public virtual void Activate()
	{
		foreach(QuestNode node in nextNodes)
		{
			node.Activate();
		}
	}


	// Custom node classes.
	#region Node Classes

	[System.Serializable]
	public class StartNode : QuestNode
	{

		public override void Activate ()
		{
			base.Activate ();
		}

		public override string ToString ()
		{
			return "Start";
		}

	}

	#endregion

}

Hello,

Currently it’s unable to serialize inherited objects with unity’s serializer. I would recommend you to store your list with XmlSerializer (System.Xml.Serialization) and read this as a TextAsset.

Another way is to create ScriptableObject with each element of your array adn one ScriptableObject as an objects holder.

Regards,
M.Rogalski

Several notes here. I’d recommend reading this (if you haven’t yet).

First thing to take into account is that you are serializing a reference of that same class (nextNodes), which is unwisely. Unity implements a 7 level depth limit when serializing, so you’re not going to get an endless loop. But it’s not going to be as performant as it should.

Second thing, Unity serialization currently doesn’t support polymorphism. It needs to know ahead of time the serialization layout.