Skip to main content

Serializable Types

A type as an ordinary field: Unity saves it, and the Inspector lets you pick it from a list.

Quick start​

Before — Unity APIAfter — FastTools
[SerializeField]
private string _primaryWeaponName;

public System.Type PrimaryWeapon =>
string.IsNullOrEmpty(
_primaryWeaponName)
? null
: System.Type.GetType(
_primaryWeaponName);
[TypeSelector(Allow = TypeAllow.None)]
[SerializeField]
private SerializableType<Weapon>
_primaryWeapon;

public System.Type PrimaryWeapon =>
_primaryWeapon;

[TypeSelector] sets the selection settings: Allow = TypeAllow.None leaves only concrete types in the list.

Selecting a serializable type in the InspectorSelecting a serializable type in the Inspector

Which wrapper to use​

TaskField
Store a type, including DLL, nested or generic typesSerializableType, SerializableType<T>
Keep the selection when your script is renamedSerializableMonoScript, SerializableMonoScript<T>

The T variant offers only types compatible with T in the picker.

SerializableType​

SerializableType stores an assembly-qualified name: the type name together with its assembly. Set a default in code:

[SerializeField]
private SerializableType<Weapon> _primaryWeapon = new(typeof(Sword));

Wrappers that store the same type name are equal, so they work as Dictionary and HashSet keys. SerializableMonoScript follows the same rule; == compares references.

Missing type​

After a class, namespace or assembly rename, the stored name no longer resolves: the field shows <Missing …> with a Missing type notice under it. The caption gives the type name without its assembly; the tooltip gives the whole stored name.

  • .Type returns null.
  • AssemblyQualifiedName and ToString() keep the old name.

The missing Game.Combat.Spear type with the Fix and → Spear buttonsThe missing Game.Combat.Spear type with the Fix and → Spear buttons

Fix opens the type picker, and the type you pick replaces the stored name. When the class only moved to another namespace or assembly and exactly one compatible type has its name, the notice also offers it, for example → Spear. The notice compares class names and does not read [MovedFrom], so a class renamed with that attribute gets no suggestion: choose the new class with Fix.

Project References finds every missing name in the project and repairs them in groups. The build check and breakage detection report new ones.

Empty field​

Test an unset field with field is null || field.IsEmpty, not field == null alone:

  • Loaded from an asset, a scene or a prefab, a field holds a wrapper, empty when nothing is picked.
  • On an object created in code (AddComponent, ScriptableObject.CreateInstance), a field is null until Unity serializes the object.

IsEmpty and IsMissing tell the states of a wrapper apart:

StateTypeIsEmptyIsMissing
A type is pickedthe typefalsefalse
Nothing is pickednulltruefalse
The stored name no longer resolves, a missing typenullfalsetrue

SerializableMonoScript​

SerializableMonoScript remembers the script asset itself, so the selection survives a script rename. Pick the type in the Inspector or drag a .cs file from Project onto the field.

After renaming Sword.cs → Blade.csSerializableTypeSerializableMonoScript
.Typenull, a missing typeBlade, in Play Mode too
Stored nameSwordBlade once the asset is saved again

Limitations:

  • only a top-level, non-generic class can be picked: one declared in a .cs file of the same name, or one derived from MonoBehaviour or ScriptableObject in a DLL;
  • there is no public constructor, so the field cannot be created in code.

The link breaks, and the field shows a missing type with the same notice, when:

  • the class is renamed without its file;
  • the file is renamed outside Unity without its .meta;
  • a class from a DLL is renamed or moved to another namespace: Unity identifies its script asset by namespace and class name.

Types in a player build​

warning

In a player, both wrappers resolve the stored type name. If a class is used only through this selection, Managed Stripping Level Medium or High strips it: .Type returns null and IsMissing becomes true, although the editor resolves it. Minimal and Low keep it.

To keep the class at Medium or High:

  • Mark the class or its constructor with [Preserve] (UnityEngine.Scripting).
  • List the class, or its whole assembly, in link.xml.
  • Mark your interface with [RequireImplementors] or your base class with [RequireDerived] to keep all its implementations: the field must be a SerializableType<T> of that interface or base class.

Package sample​

For Inspector selection of enemy types and spawn patterns, see Types.

A wave of regular and elite enemies in the Types sceneA wave of regular and elite enemies in the Types scene