Serialization of classes in (Nested) Prefab (Variance) resets to original value (worked in earlier version)

Issue #889 open
Salomon Zwecker created an issue

Odin Version: 3.1.4 (couldn’t select the right version from the dropdown)

Disclaimer

I know that you have this warning, telling that prefab serialization is officially not supported and stuff. But as this used to work in previous versions, I believe you should take a look anyway. Also, the test project (see below) might help to debug the underlying problem.

Steps to reproduce

  1. Create a project in Unity 2021.3.11f1 and import Odin 3.1.4
  2. Create an ordinary class or struct with a serialized string-variable (“TextData.cs”)
  3. Create a SerializedMonoBehaviour that declares the class of step 2 with [SerializeField] attribute, as well as a reference to a component that can display text.
    Apply the value of the class instance to the text display in Awake(). ("TextApplier.cs")
  4. Create a prefab that has the component of step 3 (TextApplier) and the required component for displaying text. Assign the values and enter a text to “TextData” (like “Original Prefab”).
  5. Create a prefab variance where the text is changed (e.g. “Prefab Variance”) and a prefab which contains the original prefab as a child. Also change the text there (e.g. “Nested Prefab”).
  6. Instantiate every one of the three prefabs in a scene (either by code or by just dragging them into the scene.
  7. Click the play button

Result

All three prefab instances have the text of the original prefab.

Note: The prefabs themselves actually still have the correct values. However, the values sometimes reset also in the project (probably: when they are in the scene and you save the scene).

Expected Result

All prefab instances have the overridden values of their prefab.

Remarks

  • If you try the steps to reproduce with Unity 2020.3.23f1 and Odin 3.0.12.0, everything works as expected.
  • If the nested prefab / prefab variance is put into another prefab (becoming another nesting-level) everything seems to work as expected. However, it seems like the values reset in the actual prefab variances / nested prefabs when saving a scene more easily if this prefab-wrapper is in the scene…

Test Project

To easily reproduce the issue, you can download two projects here:

https://we.tl/t-7amMqYSBeY

(It contains two projects: one with Odin 3.0.12.0 and one with Odin 3.1.4. Note That they both have Odin included but the 3.0.12 project doesn’t have a library folder, as it was huge).

Screenshots

Odin 3.0.12.0 Project:

Odin 3.1.4 Project:

Note that I write that a container wrapper fixes all problems. While it is still fixing the prefab variance, the inner nested prefab doesn’t seem to work. But: It worked right after I set up the wrapper Prefab. But after the prefabs resetted their values in the project ones, I couldn’t get it to work again anymore.

Personal Notes

We Upgraded our project from Unity 2020 to 2021 and were forced to also upgrade Odin. After fixing all little bugs which occurred because of the upgrade, we thought everything was fine.

But now we stumbled upon this bug, and it is game breaking for us. As it worked in the old Unity / Odin Version, we relied on it.

  • As it worked previously, I consider it a bug that is fixable and should be fixed to regain backwards compatibility. Therefore, I prioritized it as “critical”.

Comments (3)

  1. Antonio Rafael Antunes Miranda

    As we discussed in our Discord channel, we started working on a rework of the prefab serialization system which looks to be promising. It should bring back full support with no compromises. In the meantime I’m going to close this ticket. If the rework doesn’t end up fixing the prefab serialization problems we had so many times before (which is a possibility), feel free to reopen this ticket.

  2. Salomon Zwecker reporter
    • changed status to open

    I could verify that this is an Odin bug, not a unity bug. As of Tor, he would take a look as this is a regression.

    Here new instructions for reproduction:

    1. Create a unity Project and import Odin* (Unity version is probably irrelevant)
    2. Download and import the test case package: https://we.tl/t-HupVnSDr7w
    3. Open the scene TestCase/TestScene (and import TMP Essentials)
    4. Hit the play button

    *If you want to see the working version, install Odin 3.0.12.0, if you want to see the broken version, install Odin 3.1.4+. Not sure at which version it broke.

    Result: Depending on the Odin version, you either see the string that are assigned in the prefabs ("Original Prefab", "Nested Prefab", "Prefab Variant") or all of the items have the same string ("Original Prefab").

    Note that the strings are provided by the "TextApplier" component which contains a class"TextData" that holds the string.

    Also note that the strings in the actual prefabs are correct before hitting play (maybe even the ones in the scene). After hitting play, the value in the prefab might also get changed (this might depend on the unity version. In 2020.3 the prefabs remained consistent and only the scene items changed, in 2021.3 the prefab variant changed as well, but not the nested prefab. Not sure if this is deterministic behavior).

  3. Log in to comment