Skip to main content

SerializeReference Selector

Pick the field's class in the Inspector, and keep its data when the class changes.

Quick start​

Before — Unity APIAfter — FastTools
[SerializeReference]
private IWeapon _primary =
new Pistol();
[TypeSelector]
[SerializeReference]
private IWeapon _primary;

Which classes are offered​

An IWeapon field offers concrete implementations of that interface, such as Pistol and Shotgun. Selecting one creates an instance; <None> clears the field.

For extra constraints, required fields and list appearance, see TypeSelector.

  • Allow has no effect here: the types must be instantiable.
  • Classes derived from UnityEngine.Object are not offered: keep such an object in a plain field, without [SerializeReference].
  • Generic arguments are inferred from the field type and the [TypeSelector] types; when they cannot be, the picker asks for types Unity can serialize.

Lists​

In a list with [TypeSelector], “+” opens the class picker and adds a new instance, and <None> adds an empty element. With several objects selected, each gets its own instance.

“+” on Sidearms opens the class picker and adds a Shotgun“+” on Sidearms opens the class picker and adds a Shotgun

Switching the class​

When switching classes, FastTools tries to carry compatible values of fields with the same names. Fields that cannot be transferred keep the new instance's values:

FieldPistol→ Shotgun
Damage3737
Magazine Size12—
Pellets—8, the initial value

Switching from Pistol to Shotgun keeps Damage = 37Switching from Pistol to Shotgun keeps Damage = 37

A nested reference carries over as the same instance when the field name and type are compatible.

tip

You can also pick the class by dragging a .cs file from Project onto the field header.

Header menu​

Right-click the field header:

ItemWhat it does
RevertOn a prefab instance, returns a field with a changed class or data to the prefab's class and data; the label of such a field is bold. A change inside a nested reference marks only the changed field
Apply to Prefab 'Player'On a prefab instance, writes such a field into the prefab; not offered for a prefab variant selected in Project
Copy Serialize ReferenceCopies the field's class and data; the copy lasts until the next domain reload
Paste Serialize ReferencePastes the copy into a field of a compatible type; a copied empty field clears it
Find Usages of PistolSearches the project for the class through Unity Search
Create New Script…Creates a [Serializable] class for the field type and assigns it after compilation
Save as Template…Saves the value under a name; templates live in the editor settings on this machine, not in the project
Paste Template → …Creates an instance from a template; only templates that fit the field are listed
Paste Template → Remove Missing (N)…Deletes templates whose class no longer loads; shown only when such templates exist
warning

Copy/Paste and templates do not carry nested [SerializeReference] fields: the pasted object loses those references.

Shared references​

Link to Existing → … in the header menu links the field to an instance from another field on the same object.

Fields that point at one instance are marked Shared reference #N; Make unique under the field or Make Unique Reference in the header menu gives the field its own copy, nested references included.

Make unique creates an independent copy of a shared referenceMake unique creates an independent copy of a shared reference

A duplicated list element gets its own instance instead of a reference to the same one. The Auto de-alias duplicated list elements setting under Tools → Aspid 🐍 → FastTools → Settings controls this and is on by default; it is shared by the team.

This works for lists whose elements are references, such as List<IWeapon>. In a list of structs or classes that hold a [SerializeReference] field or list, Duplicate Array Element copies the reference too, so both elements share one instance until Make unique separates them.

Missing type​

After a class is renamed, moved or deleted, the field shows <Missing …> with a Missing type notice under it. The field's data stays in the asset.

The missing Game.Gear.Pistoll reference with the Fix and → Pistol buttonsThe missing Game.Gear.Pistoll reference with the Fix and → Pistol buttons

Fix opens the class picker, and the class you pick replaces the missing one. The notice may also offer a matching class, for example → Pistol; the tooltip gives the reason. What Fix keeps on an asset and in a scene is described in Fix in the Inspector.

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

Custom inspector​

In your own editor, a regular PropertyField draws a [TypeSelector] field, in UI Toolkit and in IMGUI: the class picker and the list's “+” come by themselves. For list elements drawn separately, use SerializeReferenceEditorGUI or SerializeReferenceIMGUIList.

Package sample​

For weapon selection, lists and nested references, see the SerializeReferences sample.

The dummy takes damage in the SerializeReferences sceneThe dummy takes damage in the SerializeReferences scene