Search Issue Tracker

By Design

Votes

0

Found in

2018.4

2019.3.0f6

2020.2

Issue ID

1230943

Regression

No

Prefabs with serialized variables are modified in the Inspector when changing their default values through the Script

Scene Management

-

How to reproduce:
1. Open the attached project (case 1230943.zip)
2. Open the SampleScene
3. In the Project window double click the 'Fields.cs' Script to edit it
4. Uncomment the declaration of variable 'test2' and save the changes
5. In the Project window double click the 'Cube' Prefab to enter Prefab Mode.
6. Make sure Inspector values are correct (test = 111, test2 = 222)
7. Edit the Script again by changing 'test' and 'test2' values to anything else (for example, test = 333, test2 = 444) and save the changes
8. Check the Inspector values in the Prefab Mode again (it might be needed to exit the Prefab Mode, select any other GameOject and only then re-enter Prefab Mode to see the change)

Expected result: the Inspector values remain unchanged (test1 = 111, test2 = 222)
Actual result: the newly created value 'test2' gets changed while 'test1' does not (test1 = 111, test2 = 444)

Reproducible with: 2018.4.20f1, 2019.3.7f1, 2020.1.0b2, 2020.2.0a5
Could not test with: 2017.4.38f1 (Prefab Mode not implemented)

Notes:
1. The same issue occurs with non-public values that are serialized explicitly with an attribute
2. This issue causes further problems. Clicking Reset on the Prefab Instance causes it to override (makes the font bold) the 'test2' value (test2 = 444)
3. Upon resetting the Prefab the issue no longer occurs with the specific variable (it is no longer possible to edit the Inspector value by changing its default value in the Script)
4. The same issue occurs not only with integers but with other primitive types as well

  1. Resolution Note:

    While this might seem surprising, this is a consequence of how Unity's serialization works. There is no concept of default values. Everything is just saved, regardless of whether a user changed the property or not.
    In your specific case you can follow the serialization by checking the files on disc to see the values as they are being saved and how the values are written without you modifying anything. This is why when you then change the values to 333 and 444, field1 is already serialized and any update to the script won't change the already serialized value.

Add comment

Log in to post comment

All about bugs

View bugs we have successfully reproduced, and vote for the bugs you want to see fixed most urgently.