I have scriptable objects that contain references to prefabs’ MonoBehaviours. These referenced prefabs sometimes get rebuilt by code using ReplacePrefab, so the MonoBehaviour reference get lost because the MonoBehaviour get a new fileID assigned in the Prefab file.
I then added some code that:
- save the assets (so that the new fileID gets written in the prefab file)
- write in the prefab text file directly to replace the new fileID back with the old one that is still referenced in the scriptable object.
That works, the prefab is all good and has the old fileID on the MonoBehaviour BUT for some reason the scriptable object still sees the referenced MonoBehaviour as missing, even though it has the correct fileID when I open the text-serialized asset.
There are 2 ways I found that will bring back the reference.
-
Delete the Library folder and re open the project…
-
Add a random empty change to the ScriptableObject text file from an external text editor (say a space at the end), then when back in Unity, the reference is back.
So I need any way to bring back the reference automatically. So basically just a way to make sure that the ScriptableObject will get reimported from the text file for real would work.
Reimport, Refresh, AssetDatabase.ImportAsset with ForceUpdate, all don’t work). Also adding this final space in the text file from editor code doesn’t work, probably because it needs to be done while Unity is not on focus?