- Home /
Code-generated changes to prefab override properties not persisting
Hi - I have a within and across level player transition system which uses a "Transition" prefab that has a GHTransitionController component with a unique ID. When events happen in the game, the game can reposition the player at any one of these Transition points. The unique transition ID together with the Scene Name works well.
The problem comes when I try to automatically assign GUIDs to the TransitionID of new Transition points in the editor.
I have tried using OnSceneSaving to generate the GUIDs whenever the scene is saved, which looks like it is working well (ie. I can see in the editor that the TransitionID field of any GHTransitionController which were previously blank get GUIDs whenever I Ctrl-S.
However the change doesn't seem to persist. As soon as I load another scene and reload the original, or enter Play mode, the value is reset to the original "blank".
Any thoughts? A stripped back version of my editor code is below.
using UnityEngine;
using System.Collections;
using UnityEditor.SceneManagement;
using UnityEngine.SceneManagement;
using System;
#if UNITY_EDITOR
using UnityEditor;
namespace Assets.GunheadEditors
{
[UnityEditor.InitializeOnLoad]
static class OnSaveSceneLogic
{
static OnSaveSceneLogic()
{
UnityEditor.SceneManagement.EditorSceneManager.sceneSaving += OnSceneSaving;
}
private static void OnSceneSaving(Scene scene, string path)
{
foreach (GHTransitionController tc in GameObject.FindObjectsOfType<GHTransitionController>())
{
if (tc.TransitionID == "" || tc.TransitionID == null)
{
tc.TransitionID = Guid.NewGuid().ToString();
EditorSceneManager.MarkSceneDirty(scene);
}
}
}
}
}
#endif
Further analysis - I have found that after doing a Ctrl-S and having the TransitionID visibly generated in the editor if I then ALSO manually modify any other property of the same component, the generated TransitionID change (together with the "other" change) persists. Its like Unity has a dirty flag not just on the scene, but also on objects?
I have now also tried creating a custom editor with a button which generates the ID with exactly the same behaviour. Very strange.
When I completely unpack the prefab instance, the problem goes away, so I know it is a prefab overriding issue.
However this doesn't help me as I need prefabs for this as there are loads of other components required.
The strange thing is that when I click my custom editor button, I can see the captions next to the TransitionID going BOLD, indicating that the property has been overridden when it populates. When I then go to Game mode, the bold disappears, the TransitionID is blanked, and when I stop the game the TransitionID caption goes bold again, but the value doesn't return.
Is this a bug? I am on 2021.3.2f1.