# SerializeReference Attribute?

**URL:** https://discussions.unity.com/t/serializereference-attribute/743433
**Category:** Documentation
**Tags:** 2019.3-beta
**Created:** [May 15, 2019, 2:47pm UTC](https://discussions.unity.com/t/serializereference-attribute/743433 "2019-05-15T14:47:47Z")
**Posts on this page:** 20
**Page:** 1

<div class="post-metadata">

### Author: ![SonicBloomEric](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/sonicbloomeric/32/934767_2.png) [@SonicBloomEric](https://discussions.unity.com/u/SonicBloomEric)
#### Post date: [May 15, 2019, 2:47pm UTC](https://discussions.unity.com/t/serializereference-attribute/743433/1 "2019-05-15T14:47:47Z")

</div>

Any quick word on what the **`SerializeReference` Attribute** mentioned in the release notes is?

_[Apologies in advance: no time at present to download and install the alpha and its documentation to check if it’s already documented there…]_

---

<div class="post-metadata">

### Author: ![jasonm\_unity3d](https://avatars.discourse-cdn.com/v4/letter/j/3da27b/32.png) [@jasonm\_unity3d](https://discussions.unity.com/u/jasonm_unity3d)
#### Post date: [May 15, 2019, 3:05pm UTC](https://discussions.unity.com/t/serializereference-attribute/743433/2 "2019-05-15T15:05:41Z")

</div>

The field attribute is for serializing reference types (c# class, not structs) by reference as opposed to by value (as is the default behaviour).

This allows:

- Expressing graphs without having to resort to ScritableObjects for each node in the graph.
- Polymorphic data, ex: List;
- Have null fields: Standard deserialization behaviour for fields that are null is to set them to a default value of the type.

contrived usage example:

```csharp
interface INode{ }

[Serializable]
class RootNode : INode
{
   [SerializeReference] public INode left;
   [SerializeReference] public INode right;
}

[Serializable]
class SubNode : RootNode
{
   [SerializeReference] INode parent;
}

[Serializable]
class LeafNode : INode
{
   [SerializeReference] public INode parent;
}

class SomeThing : ScriptableObject
{
    [SerializeReference]
    public List<RootNode> m_Trees;
}

```

---

<div class="post-metadata">

### Author: ![Alex-Chouls](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/alex-chouls/32/83430_2.png) [@Alex-Chouls](https://discussions.unity.com/u/Alex-Chouls)
#### Post date: [May 15, 2019, 3:44pm UTC](https://discussions.unity.com/t/serializereference-attribute/743433/3 "2019-05-15T15:44:46Z")

</div>

This is very cool!

---

<div class="post-metadata">

### Author: ![SonicBloomEric](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/sonicbloomeric/32/934767_2.png) [@SonicBloomEric](https://discussions.unity.com/u/SonicBloomEric)
#### Post date: [May 15, 2019, 3:54pm UTC](https://discussions.unity.com/t/serializereference-attribute/743433/4 "2019-05-15T15:54:17Z")

</div>

Sweet, sweet heavens. @jasonm_unity3d that is _ **huge** _. Is there going to be more information on this, perhaps posted in the blog? It seems to me that an update to the [Serialization in Unity](https://blogs.unity3d.com/2014/06/24/serialization-in-unity/) blog entry would be super handy. (This assumes [the manual](https://docs.unity3d.com/Manual/script-Serialization.html) will get its own update, of course.)

---

<div class="post-metadata">

### Author: ![jasonm\_unity3d](https://avatars.discourse-cdn.com/v4/letter/j/3da27b/32.png) [@jasonm\_unity3d](https://discussions.unity.com/u/jasonm_unity3d)
#### Post date: [May 15, 2019, 5:59pm UTC](https://discussions.unity.com/t/serializereference-attribute/743433/5 "2019-05-15T17:59:28Z")

</div>

A blog post is in the works, but I’m not in the loop as far as the eta.  
The API documentation is ready but has yet to be published, docs team is looking into as we speak.

Note that [SerializedReference] does come with a slight performance hit, hard to measure as it depends on context. But it’s there, so it is not advised for use for the general cases (where in general, performance considerations trump just about everything else).

But for those rare cases where the implementation/maintenance complexity does trump performance or it’s a non issue as the final objects are not that big so they won’t have measurable impact even if they are slower to deserialize, then this feature fits the bill.

---

<div class="post-metadata">

### Author: ![CDF](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/cdf/32/102324_2.png) [@CDF](https://discussions.unity.com/u/CDF)
#### Post date: [May 15, 2019, 6:14pm UTC](https://discussions.unity.com/t/serializereference-attribute/743433/6 "2019-05-15T18:14:31Z")

</div>

It’s… Beautiful

![4544590--421621--tumblr_mzcos91INP1qd2l0fo1_250.gif](https://europe1.discourse-cdn.com/unity/original/3X/b/6/b6c9f60af096a0b7d1ec7fa3ac49d7256a6e522f.gif)

---

<div class="post-metadata">

### Author: ![SonicBloomEric](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/sonicbloomeric/32/934767_2.png) [@SonicBloomEric](https://discussions.unity.com/u/SonicBloomEric)
#### Post date: [May 15, 2019, 6:27pm UTC](https://discussions.unity.com/t/serializereference-attribute/743433/7 "2019-05-15T18:27:33Z")

</div>

> [@jasonm\_unity3d](#):
>
> Note that [SerializedReference] does come with a slight performance hit, hard to measure as it depends on context. But it’s there, so it is not advised for use for the general cases (where in general, performance considerations trump just about everything else).

Can you quantify “performance hit” in some way?

We’ll have to benchmark our own code, of course, in the long run. We _ **already** _ serialize references with Serialization Callback shenanigans. My fingers are crossed that the new built-in approach is at least as fast as our custom solution…

---

<div class="post-metadata">

### Author: ![jasonm\_unity3d](https://avatars.discourse-cdn.com/v4/letter/j/3da27b/32.png) [@jasonm\_unity3d](https://discussions.unity.com/u/jasonm_unity3d)
#### Post date: [May 15, 2019, 7:39pm UTC](https://discussions.unity.com/t/serializereference-attribute/743433/8 "2019-05-15T19:39:07Z")

</div>

the extra overhead is impacted by how many types are involved and how many objects.  
In some scenarios, you I have seen slight speed ups (large graphs, small nodes), in other scenarios i’ve seen slowdown: Many small ScriptableObjects with one small field that is serialized by reference.

The nature of the data model being serialized has to much of an impact to be able to say: “you can expect x% slow down, y% speed up”.

Purely anecdotally, one small lists (~20 items) with 2-3 types involved and 4-5 fields per type, one test run that I remember was .5% slower. But again, that was a synthetic test case and doesn’t mean much. Will be very interesting to hear actual reports come back from our users for real world usage…

---

<div class="post-metadata">

### Author: ![recursive](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/recursive/32/568336_2.png) [@recursive](https://discussions.unity.com/u/recursive)
#### Post date: [May 15, 2019, 7:49pm UTC](https://discussions.unity.com/t/serializereference-attribute/743433/9 "2019-05-15T19:49:02Z")

</div>

Will this serialize dictionaries, mainly because that’s an old request and we can finally put it to bed if this solves it?

---

<div class="post-metadata">

### Author: ![dotsquid](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/dotsquid/32/582002_2.png) [@dotsquid](https://discussions.unity.com/u/dotsquid)
#### Post date: [May 16, 2019, 7:45am UTC](https://discussions.unity.com/t/serializereference-attribute/743433/10 "2019-05-16T07:45:02Z")

</div>

Sweet Lord, at last!  
_runs in circles in tears of happiness_

---

<div class="post-metadata">

### Author: ![DoctorShinobi](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/doctorshinobi/32/243002_2.png) [@DoctorShinobi](https://discussions.unity.com/u/DoctorShinobi)
#### Post date: [May 16, 2019, 8:21am UTC](https://discussions.unity.com/t/serializereference-attribute/743433/11 "2019-05-16T08:21:03Z")

</div>

Holy shit. Does that mean we’ll be able to serialize interfaces? I’ll finally be able to use interfaces without feeling like Unity is fighting me by not letting me reference them in the inspector.

---

<div class="post-metadata">

### Author: ![SirIntruder](https://avatars.discourse-cdn.com/v4/letter/s/53a042/32.png) [@SirIntruder](https://discussions.unity.com/u/SirIntruder)
#### Post date: [May 16, 2019, 8:28am UTC](https://discussions.unity.com/t/serializereference-attribute/743433/12 "2019-05-16T08:28:26Z")

</div>

Nice!  
Does this means we will also get a way to get System.Object reference from SerializedProperty?  
pretty please 😄

---

<div class="post-metadata">

### Author: ![Prodigga](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/prodigga/32/534237_2.png) [@Prodigga](https://discussions.unity.com/u/Prodigga)
#### Post date: [May 16, 2019, 12:16pm UTC](https://discussions.unity.com/t/serializereference-attribute/743433/13 "2019-05-16T12:16:31Z")

</div>

Wow! I can’t believe this isn’t mentioned in [Preview of 2019.3 Features](https://discussions.unity.com/t/743421)

Serialisation support for references _and_ polymorphic data? Are you kidding? It is always such a massive pain to write complex tools for unity because you need to restructure your data to work around Unity’s serializer. This will make things so much easier.

---

<div class="post-metadata">

### Author: ![Prodigga](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/prodigga/32/534237_2.png) [@Prodigga](https://discussions.unity.com/u/Prodigga)
#### Post date: [May 16, 2019, 1:08pm UTC](https://discussions.unity.com/t/serializereference-attribute/743433/14 "2019-05-16T13:08:20Z")

</div>

After some messing about, it seems very flexible _so long as_ you are exclusively using custom classes. Also, (kind of expected) the inspector gets really weird really quickly and is frequently causes crashes when editing values of data that has been tagged as SerialiseReference. (It _really_ likes crashing the editor!) The data _does_ serialize and deserialize correctly - it’s just that the rendering is a little broken in the inspector.

If you need complex data structures like this that support reference serialization then you are probably putting together something relatively complex, so you will probably be manipulating this data with a custom editor of some sort…so its not such a big deal! In my test, I was able to ‘poke’ the data on my monobehaviour using ‘ContextMenu’ methods and verify that references are maintained as expected.

Overall, very very very cool! It _is_ a shame that we still can’t reference UnityEngine.Object’s via their interfaces though…! But at this point I’ll take what I can get…!

---

<div class="post-metadata">

### Author: ![M\_R](https://avatars.discourse-cdn.com/v4/letter/m/bcef8e/32.png) [@M\_R](https://discussions.unity.com/u/M_R)
#### Post date: [May 16, 2019, 1:45pm UTC](https://discussions.unity.com/t/serializereference-attribute/743433/15 "2019-05-16T13:45:20Z")

</div>

> [@Prodigga](#):
>
> … we still can’t reference UnityEngine.Object’s via their interfaces …

ouch. this is exactly what I wanted it to do (being able to code against interfaces and the implementation can be either a MonoBehaviour or a ScriptableObject…)

---

<div class="post-metadata">

### Author: ![AlkisFortuneFish](https://avatars.discourse-cdn.com/v4/letter/a/848f3c/32.png) [@AlkisFortuneFish](https://discussions.unity.com/u/AlkisFortuneFish)
#### Post date: [May 16, 2019, 3:11pm UTC](https://discussions.unity.com/t/serializereference-attribute/743433/16 "2019-05-16T15:11:46Z")

</div>

Hell is officially affected by climate change!

---

<div class="post-metadata">

### Author: ![jasonm\_unity3d](https://avatars.discourse-cdn.com/v4/letter/j/3da27b/32.png) [@jasonm\_unity3d](https://discussions.unity.com/u/jasonm_unity3d)
#### Post date: [May 16, 2019, 3:44pm UTC](https://discussions.unity.com/t/serializereference-attribute/743433/17 "2019-05-16T15:44:02Z")

</div>

- API link: [Unity - Scripting API: SerializeReference](https://docs.unity3d.com/2019.3/Documentation/ScriptReference/SerializeReference.html)

- UnityEngine.Object cannot be stored in fields with the [SerializeReference] attribute, so the interface restriction remains. I don’t expect that limitation to ever be lifted as it’s actually a fundamental design limitation of who the serialization system is leveraged all over the unity code base.

- Unfortunately no, Dictionary serialization is still not supported.

- `Does this means we will also get a way to get System.Object reference from SerializedProperty?`  
Not sure what is ment by that but, you can have `[SerializeReference] List<System.Object> data;`. You can then access it through serialziedObject/Property and fetch the fields from the instance’s class not just the System.Object class. If that makes sense?

- `Does that mean we'll be able to serialize interfaces? `  
Yes, but will reject objects that are descendants of UnityEngine.Object.

And, if you happy about this feature: Please, please, please, be diligent in reporting any and all bugs through the editor as this feature was very invasive and will need love to stabilize and be rock solid.

cheers  
-J

---

<div class="post-metadata">

### Author: ![AlkisFortuneFish](https://avatars.discourse-cdn.com/v4/letter/a/848f3c/32.png) [@AlkisFortuneFish](https://discussions.unity.com/u/AlkisFortuneFish)
#### Post date: [May 16, 2019, 3:57pm UTC](https://discussions.unity.com/t/serializereference-attribute/743433/18 "2019-05-16T15:57:52Z")

</div>

> [@jasonm\_unity3d](#):
>
> - API link: [https://docs.unity3d.com/2019.3/Documentation/ScriptReference/SerializeReference.html](https://docs.unity3d.com/2019.3/Documentation/ScriptReference/SerializeReference.html)

> [@](#):
>
> - Generic Lists and array fields decorated with [SerializeReference] apply the attribute to the elements of the list/array not the list/array instance itself.

I have wondered for a while, whether there should be some way to apply an attribute to a collection, rather than the elements. Ever since the change was made to apply attributes to the elements of the collection, there has been no way to apply attributes to a collection itself, requiring custom inspectors to override collection drawing behaviour, which was annoying but not a showstopper.

Now that there’s an attribute that fundamentally alters serialization to this extent, it sounds like this should be looked at.

Granted, if I must serialise a collection by internal reference I could wrap it in a class, but it would be great not to have to.

---

<div class="post-metadata">

### Author: ![Charles\_Beauchemin](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/charles_beauchemin/32/44210_2.png) [@Charles\_Beauchemin](https://discussions.unity.com/u/Charles_Beauchemin)
#### Post date: [May 16, 2019, 4:45pm UTC](https://discussions.unity.com/t/serializereference-attribute/743433/19 "2019-05-16T16:45:04Z")

</div>

> [@Prodigga](#):
>
> Wow! I can’t believe this isn’t mentioned in [https://discussions.unity.com/t/743421](https://discussions.unity.com/t/743421)

good point! I added it. And I will add hyperlinks as soon as I get some preliminary documentation.

---

<div class="post-metadata">

### Author: ![jasonm\_unity3d](https://avatars.discourse-cdn.com/v4/letter/j/3da27b/32.png) [@jasonm\_unity3d](https://discussions.unity.com/u/jasonm_unity3d)
#### Post date: [May 16, 2019, 4:46pm UTC](https://discussions.unity.com/t/serializereference-attribute/743433/20 "2019-05-16T16:46:36Z")

</div>

> [@AlkisFortuneFish](#):
>
> I have wondered for a while, whether there should be some way to apply an attribute to a collection, rather than the elements. Ever since the change was made to apply attributes to the elements of the collection, there has been no way to apply attributes to a collection itself, requiring custom inspectors to override collection drawing behaviour, which was annoying but not a showstopper.
> 
> Now that there’s an attribute that fundamentally alters serialization to this extent, it sounds like this should be looked at.
> 
> Granted, if I must serialise a collection by internal reference I could wrap it in a class, but it would be great not to have to.

The initial proposal for Serializing References did support that specific use case, but it was later rejected during peer review: was deemed a ‘nice to have’ but not common enough to be worth the added dev time & complexity.

If enough people ask for it, who knows…

[Next page](https://discussions.unity.com/t/serializereference-attribute/743433.md?page=2)
