How to trigger actual field renaming In embedded instances of ScriptableObject asset files while using [FormerlySerializedAs] attribute?

Unity advertises the [FormerlySerializedAs] attribute as the final solution for renaming serializable fields. Here are the steps that are officially recommended to do:

  1. Add [FormerlySerializedAs] attribute with the current field name to the field.
  2. Rename the field (I use MonoDevelop refactoring)
  3. Re-save the assets.
  4. Remove the [FormerlySerializedAs] attribute.

The problem I have is between (3) and (4). It doesn’t quiet work. Here is my set-up:

The deepest instance’s class:

[Serializable]
public class B // Before
{
	[SerializeField] private string oldString1;
	[SerializeField] private string oldString2;
}

The middle-depth instance’s class:

[Serializable]
public class A
{
	[SerializeField] private B _b;
}

The actual ScriptableObject instance’s class:

[CreateAssetMenu]
public class MyAsset : ScriptableObject
{
	[SerializeField] private A _a;
}

Here is how the inspector and also the actual (text-serialized) asset file look. I combined two screenshots in one because Unity Answers limits number of attachments.

52993-1.png

Now what I do is follow the official steps recommended by Unity for the oldString1 and oldString2 fields of B class:

[Serializable]
public class B
{
	[FormerlySerializedAs("oldString1")] [SerializeField] private string newString1;
	[FormerlySerializedAs("oldString2")] [SerializeField] private string newString2;
}

In the inspector, as expected, what I see is the the new variables holding old values. So far so good. But the actual asset file (I used MacOS preview on pressing Space) still holds the old field names! Things I’ve tried more than multiple times: saving scene, saving project, refreshing assets, reimporting this asset, reimporting all assets, relaunching Unity. To no avail.

52996-2.png

Needless to say, if I now follow the official step (4) and remove the now-supposedly-useless [FormerlySerializedAs] attributes, the values are lost. They are lost, because now there is inconsistence between field names in the script and field names in the asset file.

Have I discovered a bug, or am I missing something? Interestingly enough, while my example doesn’t work, simple scenarios with game objects originates in a scene and no embedded nesting all work just fine. Both the inspector and the actual file would hold the new field names.

How to do I trigger the actual field renaming in the embedded instances of file-serialized ScriptableObject assets?

The only thing that helped is manually go into the field in Inspector, edit it and press Enter. Unfortunately it’s not an option for me, because of incredible amount of assets the field renaming is involving. I want automation, not opening the inspector by hand on a thousand files.

Big thanks for a thorough answer.

better late than never …
with this snippet of code you can get manual set dirty feature (usefull to trigger re-serialization after formerlyserializedas refacto)

HowTo: make a find on your type (“t:MyAsset” in fuzzy finder), select all elements found, right click → tools → set dirty, then make a ctrl+s and all yours selected asset where correctly saved with refactored names.

	[MenuItem("Assets/Tools/Set Dirty")]
	private static void SetDirty() {
		foreach (Object o in Selection.objects) {
			EditorUtility.SetDirty(o);
		}
	}