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.