Losing SerializedField references when scene is unloaded in editor.

I wrote a simple tool to grab the references to two rooms that are offset from the gameObjects transform and using an Physics.OverlapBox which can be executed in the editor by clicking the “Detect” button:

This “works” at first glance:

But when the scene is unloaded in the editor, the generated references are lost. However if i manually drag the references into their respective fields, the references will hold:

Now i notice that since this component is on a prefab, that the dragged references are bold since they are not part of the prefab but rather part of the prefab instance in the scene. The generated ones(That get lost when unloading the scene) are not bold but also not part of the prefab but the prefab instance.

Here is a stripped down version of the code:

    public class DoorDiscoveryMapItem : MonoBehaviour
    {
        [SerializeField] private RoomHelper m_roomA;
        [SerializeField] private RoomHelper m_roomB;
        [SerializeField, MQNTools.Editor.InspectorButton(nameof(DetectRooms))]
        private bool m_detect;
       
        // .. Stripped down
       
        private void DetectRooms()
        {
            m_roomA?.DetectRoom(transform);
            m_roomB?.DetectRoom(transform);
        }


        [System.Serializable]
        public class RoomHelper
        {
            [field: SerializeField]
            public RoomDiscoveryMapItem RoomDiscoveryMapItem { get; private set; }
            // .. Stripped down
           
            public void DetectRoom(Transform _transform)
            {
                Vector3 position = _transform.position + DetectionZone;
                Collider[] colliders = Physics.OverlapBox(position, s_overlapeBoxSize * 0.5f);
                if (colliders != null)
                {
                    if (colliders[0].TryGetComponent<RoomDiscoveryMapItem>(out var roomDiscoveryMapItem))
                    {
                       RoomDiscoveryMapItem = roomDiscoveryMapItem;
                    }
                }
            }
        }
    }

NOTE: The MQNTools.Editor.InspectorButton is a custom attribute which simply displays a button and calls the method defined.

Does anyone know how I can get the generated references to “stick”? I have several doors and it would be a pain to comb through the geometry and manually assign these references.

Thanks in advance.

Probably just need to dirty the object: Unity - Scripting API: EditorUtility.SetDirty

Whenever you change the values of an asset via code, you need to tell Unity that something has changed and needs to be written to disk. Usually done with either SetDirty, or the Undo system.

Perfect. Thank you. I refactored the DetectRooms() method a bit:

       private void DetectRooms()
        {
            bool isDirtyA = Detect(m_roomA);
            bool isDirtyB = Detect(m_roomB);

#if UNITY_EDITOR
            if (isDirtyA || isDirtyB)
            {
                EditorUtility.SetDirty(this);
            }
#endif
            bool Detect(RoomHelper _room)
            {
                if (_room == null) { return false; }
                _room.DetectRoom(transform);
                return true;
            }
        }

And it works great now. Thanks again.

For future reference and other readers:

You can spot such an issue easily by first saving the scene.
Then make the change that doesn’t get saved.
Observe whether the scene’s root object gets the asterisk appended.

This denotes that the scene has “unsaved changes”.
9553981--1350334--upload_2023-12-30_10-21-56.png

If the asterisk doesn’t appear you have a change that isn’t registered as a change because the object didn’t get flagged as dirty.

Of course, if you save the scene and it retains the asterisk you have the inverse issue: something in the scene gets changed upon saving the scene (or every update), possibly by applying scripted changes in an event method.

Since it looks like this code gets executed in reaction to developer input, you might want to use Undo.RecordObject instead of EditorUtility.SetDirty. It will not only handle dirtying the scene for you if any changes are detected, but will also make it possible to undo the operation, if e.g. the button is clicked by mistake.