UI not serializing on play?

I have a script that runs in edit mode that automatically orders a list of objects on a different script and will auto populate it if new objects of that type are added to that scene.
However, I noticed that, despite it being updated in the inspector, once the scene starts, it reverts back to the version of the list from when I last interacted with the list in the inspector.

For example, if I have an list with 3 objects and I reorder them in the list in the inspector, then add 2 more objects the scene, which causes my code to add them to the list automatically. When I hit play the list will be reverted to the version with only 3 objects in it from when I last interacted with the list in the inspector, despite it displaying the correct list in edit mode.

Running setDirty on an editor script fixes it, however, I have to still physically select the object with the UI in the inspector to get setDirty to trigger. Is there a better solution?

Here’s a quick example:

[ExecuteAlways]
public class ListFixer : MonoBehaviour
{
    public OtherScript otherScript;
void Update()
    {
#if UNITY_EDITOR

        if (!Application.IsPlaying(gameObject))
        {
           otherScript.listObjects = otherScript.listObjects.Where(item => item != null).ToList();
           otherScript.listObjects = otherScript.listObjects.Distinct<Checkpoint>().ToList();
       }
}
}

In the full version you can reorder the listObjects, but it fails to update in this basic implementation as well. It also fails to update if I put the list on the script that’s updated in edit mode.

Yes, that’s how Play mode works. The runtime is a separate environment and everything resets once you quit, for your safety. That’s a feature, not a bug. If you want serialized data to persist, hook up SerializedObjects instead.

I’m not sure if I’m misunderstanding, but my issue is that my changes made using my script during edit mode don’t persist when going into play mode.

You need to inform Unity that the objects have changed, otherwise your changes will be lost. You can do this either by recording the change with the Undo class, or dirty it with EditorUtility.SetDirty.

Sorry, I haven’t noticed it was an editor script.

Simply reloading the scene would probably confirm this as well. It’s the same root cause. You need to force the editor to serialize this information somehow.