Hello, I have a problem where I can’t find a solution:
I’m adding objects to a list on a script on a prefab with this code:
if (UnityEditor.Experimental.SceneManagement.PrefabStageUtility.GetCurrentPrefabStage() != null) // In Prefab Mode
{
RoomConfig currentConfig = UnityEditor.Experimental.SceneManagement.PrefabStageUtility.GetCurrentPrefabStage().FindComponentOfType<RoomConfig>();
string path = UnityEditor.Experimental.SceneManagement.PrefabStageUtility.GetPrefabStage(Selection.activeObject as
GameObject).assetPath; // Get the prefab path in disk
GameObject prefabRoot = PrefabUtility.LoadPrefabContents(path); // Load the contents of the prefab.
SerializedObject saveConfig = new SerializedObject(currentConfig);
currentConfig.AddPositionMarker(this); // Add a reference to this object to the RoomConfig
saveConfig.Update();
saveConfig.ApplyModifiedProperties();// Apply after editing so that the values are stored
try
{
PrefabUtility.SaveAsPrefabAsset(prefabRoot, path); // Save the prefab to disk
Debug.Log("Saving prefab");
}
finally
{
PrefabUtility.UnloadPrefabContents(prefabRoot); // Unload to clear memory..
}
}
else
{
RoomConfig currentConfig = FindObjectOfType<RoomConfig>();
currentConfig.AddPositionMarker(this);
EditorUtility.SetDirty(currentConfig);
}
Now this works fine in the Prefab Editor. I can see the added reference in the public field of the RoomConfig. (Yes I made sure it’s public and gets saved ^^)
However when I instatiate the Prefab during Runtime I have this weird behaviour:
It works the first time just fine. But when I start the Scene a second time the Prefab loses the added references.
When I drag the prefab into the current scene the added fields get automatically flagged as “changed” by the Editor. (Bold font and listed in the override list).
What am I doing wrong? Or is it a bug?