Unity-Technologies/UnityCsReference · error · ArgumentException

object is not a EditorBuildSettingsScene

Error message

object is not a EditorBuildSettingsScene

What it means

Thrown by EditorBuildSettingsScene.CompareTo when the argument is not an EditorBuildSettingsScene. The method implements IComparable.CompareTo(object) but only handles its own type; any other type (or a subtype the cast misses) yields ArgumentException. This is the classic IComparable contract violation — CompareTo must accept object and reject incompatible types cleanly.

Source

Thrown at Editor/Mono/EditorBuildSettings.bindings.cs:63

        public bool enabled { get { return m_enabled; } set { m_enabled = value; } }
        public string path { get { return m_path; } set { m_path = value.Replace("\\", "/"); } }
        public GUID guid { get { return m_guid; } set { m_guid = value; } }
        public static string[] GetActiveSceneList(EditorBuildSettingsScene[] scenes)
        {
#pragma warning disable UA2001 // The Banned API Analyzer produces compile errors for any new Linq code. This pre-existing usage has been suppressed, but should be rewritten if possible.
            return scenes.Where(scene => scene.enabled && !string.IsNullOrEmpty(scene.path)).Select(scene => scene.path).ToArray();
#pragma warning restore UA2001
        }

        public int CompareTo(object obj)
        {
            if (obj is EditorBuildSettingsScene)
            {
                EditorBuildSettingsScene temp = (EditorBuildSettingsScene)obj;
                return temp.path.CompareTo(path);
            }
            throw new ArgumentException("object is not a EditorBuildSettingsScene");
        }

        [RequiredByNativeCode, UsedImplicitly]
        private static void DeconstructArrayElement(EditorBuildSettingsScene[] arr, int index, out bool enabled, out string path, out GUID guid)
        {
            var item = arr[index];
            enabled = item.enabled;
            path = item.path;
            guid = item.guid;
        }
    }

    [global::UnityEngine.NativeClass("EditorBuildSettings", PersistentTypeId = 1045)]
    [NativeHeader("Editor/Src/EditorBuildSettings.h")]
    public partial class EditorBuildSettings : UnityEngine.Object
    {
        private EditorBuildSettings() {}
        [AutoStaticsCleanupOnCodeReload]

View on GitHub (pinned to 225b0fbdb5)

Solutions

  1. Ensure the collection contains only EditorBuildSettingsScene before sorting; filter or separate by type.
  2. If you must compare mixed types, implement a custom IComparer<object> that checks type first and returns 0/orders by type.
  3. Call CompareTo only with values you have type-checked: if (obj is EditorBuildSettingsScene other) ... else throw explicitly.

Example fix

// before
Array.Sort(mixedObjects); // contains non-scene items
// after
var scenes = mixedObjects.OfType<EditorBuildSettingsScene>().ToArray();
Array.Sort(scenes, (a, b) => string.Compare(a.path, b.path, StringComparison.Ordinal));
Defensive patterns

Strategy: type-guard

Validate before calling

if (obj is EditorBuildSettingsScene other)
    return other.path.CompareTo(path);
throw new ArgumentException($"Expected EditorBuildSettingsScene, got {obj?.GetType()}");

Type guard

static bool IsEditorBuildSettingsScene(object o) => o is EditorBuildSettingsScene;

Prevention

When it happens

Trigger: Placing EditorBuildSettingsScene instances into a Sort() call alongside other types, or passing a boxed value of a different type to CompareTo directly. Also triggered by generic containers/sorters that call CompareTo(object) without type narrowing.

Common situations: Custom build-settings UI that sorts a heterogeneous list; interop with arrays typed as object[]; reflection-driven code invoking CompareTo with the wrong runtime type.

Related errors


AI-assisted analysis of Unity-Technologies/UnityCsReference@225b0fbdb5 (2026-08-13). Data as JSON: /api/errors/1c13608841c3a90b. Report an issue: GitHub.