Search Issue Tracker
By Design
Votes
0
Found in
2019.4
2020.3
2021.1
2021.1.3f1
2021.2
Issue ID
1329806
Regression
No
SerializeReference fields do not serialize any value when set with an integer instead of an instance of a serializable class.
Reproduction steps:
1. Open the attached project ("1329806R.zip")
2. Open the "SampleScene" scene
3. Add "BugScript" script to any Scene Game object
4. Open the Console and observe the Value
Expected result: Value persists
Actual result: Value is set to 0 (see "2p6VXycdC3.mp4" video)
Reproducible with: 2019.4.27f1, 2020.3.12f1, 2021.1.10f1, 2021.2.0a18
Not reproducible with: 2018.4.35f1 (The type or namespace name 'SerializeReferenceAttribute' could not be found (are you missing a using directive or an assembly reference?))
Note:
- Type of SerializeReference persists
Add comment
All about bugs
View bugs we have successfully reproduced, and vote for the bugs you want to see fixed most urgently.
Latest issues
- Frame Debugger's Hierarchy is not navigable when connecting it to the project built with the Volumetric Fog
- The number of SetPass Calls is increasing when attaching the Frame Debugger to the Player
- Scrollbar briefly appears in the Package Manager during installation process even when the package list is too short to require scrolling
- Script resets to use the previous Skybox color when saving the Scene changes
- [2D] Sprite Library Editor window throws NullReferenceException error when entering Play Mode with Game View maximised
Resolution Note (2021.2.X):
SerializeReference is intended for serializing classes and it applies the Unity serialization rules to that class.
In the case of an "int" this gets boxed into a object and Unity records the type as Int32, this is visible in the YAML if you save the scene. However there are no public fields in the Int32 class, so unity does not actually save the value of the int. The same would be observed with putting a raw string into an [SerializeReference]. So the behaviour is actually expected because there is nothing to serialize based on Unity rules.
To serialize an "int," technically a little wrapper class could be used instead of the system Int32 class that was automatically instantiated. However SerializeReference implies a significant overhead in both in memory and in serialized data which is disproportionate to the size of an integer. So for serializing individual small values like this best to try to serialize it as a regular field.
We've revised the reference for SerializeReference in recent release and hope it is a bit more clear now that it is intended for classes, not basic types, structs, but based on this ticket may spell it out more clearly in a future revision.