Skip to main content

SerializeReferences Sample

A turret whose weapons and their effects are picked in the Inspector.

The dummy changes color and shrinks as the configured weapons and effects deal damage.The dummy changes color and shrinks as the configured weapons and effects deal damage.

The dummy changes color and shrinks as the configured weapons and effects deal damage.

Open it​

  1. Import the sample: Tools → Aspid 🐍 → FastTools → Welcome → Samples → Import on SerializeReferences.
  2. Open Scenes/SerializeReferences.unity and enter Play Mode: the primary weapon and the sidearms take turns hitting the dummy.

Try​

Exit Play Mode and select Loadout.

  1. Pick a class. Open the Primary dropdown: the picker lists every IWeapon class, and the picked class shows its fields under the dropdown.
  2. Values survive a switch. In Sidearms, set the Pistol's Damage to 37, switch it to Shotgun and back: Damage is still 37, because both classes have _damage.
  3. Lists. Press + on Sidearms: the picker opens, and the new element gets its own instance.
  4. Nested references. The Railgun in Primary has its own [SerializeReference] field, Charge Effect, with a BurnEffect: the dummy catches fire when the railgun hits.
  5. Abstract base. On Hit is a StatusEffect: the picker offers BurnEffect and FreezeEffect, never the abstract class.
  6. Header menu. Right-click a field header for copy, paste and more: every item.

Where to look​

FileShows
Scripts/Loadout.cs[TypeSelector] on a single field, a list and an abstract base
Scripts/Weapons/Railgun.csA nested [SerializeReference]

Reference: SerializeReference Selector.