Version: 2019.3
  • C#


class in UnityEngine


Implemented in:UnityEngine.CoreModule

Suggest a change


Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.


Submission failed

For some reason your suggested change could not be submitted. Please <a>try again</a> in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.




Instruct Unity to serialize a field as a reference.

When Unity serializes an object, it serializes all fields as value types, unless the field type derives from [UnityEngine.Object]. By default, polymorphic fields are not supported and reference based topologies, like graphs, cannot be expressed natively.

It is recommended to derive the field type from ScriptableObject since this usually results in the best performance.

However, if using ScriptableObjects adds unacceptable complexity, decorating the field with a [SerializeReference] instructs Unity to serialize the field 'by reference' instead of 'by value'.

Notes: - The field type must not be of a type that specializes UnityEngine.Object.
- The field type can be abstract.
- The field type can be an interface.
- Generic Lists and array fields decorated with [SerializeReference] apply the attribute to the elements of the list/array not the list/array instance itself.
- Referenced values cannot be shared between UnityEngine.Object instances. For example, two MonoBehaviours cannot share an object that is serialized by reference. Use ScriptableObjects instead to share data.
- Field value can be null.

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

public interface IShape {}

[Serializable] public class Cube : IShape { public Vector3 size; }

[ExecuteInEditMode] public class BuildingBlocks : MonoBehaviour { [SerializeReference] public List<IShape> inventory;

void OnEnable() { if (inventory == null) { inventory = new List<IShape>() { new Cube() {size = new Vector3(1.0f, 1.0f, 1.0f)} }; Debug.Log("Created list"); } else Debug.Log("Read list"); } }

Did you find this page useful? Please give it a rating: