Unity-Technologies/UnityCsReference · error · Exception

Cannot add non-persisted config object with name '${name}'.

Error message

Cannot add non-persisted config object with name '${name}'.

What it means

Thrown by EditorBuildSettings.AddConfigObject when the native call returns ConfigObjectResult.FailedNonPersistedObj — the passed UnityEngine.Object is not an asset (not persisted to disk / not tracked by AssetDatabase). Config objects must reference persistent assets so they survive serialization; transient Scene/GameObject instances or 'new' ScriptableObjects are rejected.

Source

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

        [Obsolete("UseParallelAssetBundleBuilding is obsolete and will be removed.")]
        [NoAutoStaticsCleanup] // deprecated value-type setting; safe to persist across reload
        public static bool UseParallelAssetBundleBuilding { get; set; } = false;

        [NativeMethod("AddConfigObject")]
        static extern ConfigObjectResult AddConfigObjectInternal(string name, Object obj, bool overwrite);
        public static extern bool RemoveConfigObject(string name);
        public static extern string[] GetConfigObjectNames();
        static extern Object GetConfigObject(string name);
        public static void AddConfigObject(string name, Object obj, bool overwrite)
        {
            var result = AddConfigObjectInternal(name, obj, overwrite);
            if (result == ConfigObjectResult.Succeeded)
                return;
            switch (result)
            {
                case ConfigObjectResult.FailedEntryExists: throw new Exception("Config object with name '" + name + "' already exists.");
                case ConfigObjectResult.FailedNonPersistedObj: throw new Exception("Cannot add non-persisted config object with name '" + name + "'.");
                case ConfigObjectResult.FailedNullObj: throw new Exception("Cannot add null config object with name '" + name + "'.");
            }
        }

        public static bool TryGetConfigObject<T>(string name, out T result) where T : Object
        {
            result = GetConfigObject(name) as T;
            return result != null;
        }
    }
}

View on GitHub (pinned to 225b0fbdb5)

Solutions

  1. Persist the object first: AssetDatabase.CreateAsset(obj, "Assets/MyConfig.asset"); before AddConfigObject.
  2. If the object already exists on disk, resolve the asset reference with AssetDatabase.LoadAssetAtPath rather than a scene instance.
  3. Ensure you pass the asset itself, not an instance/spawn of it.

Example fix

// before
var cfg = ScriptableObject.CreateInstance<MyConfig>();
EditorBuildSettings.AddConfigObject("cfg", cfg, false); // not persisted
// after
var cfg = ScriptableObject.CreateInstance<MyConfig>();
AssetDatabase.CreateAsset(cfg, "Assets/MyConfig.asset");
AssetDatabase.SaveAssets();
EditorBuildSettings.AddConfigObject("cfg", cfg, overwrite: true);
Defensive patterns

Strategy: validation

Validate before calling

if (obj == null || string.IsNullOrEmpty(AssetDatabase.GetAssetPath(obj)))
    throw new InvalidOperationException("obj must be a persisted asset");
EditorBuildSettings.AddConfigObject(name, obj, overwrite: true);

Type guard

static bool IsPersistedAsset(UnityEngine.Object o) =>
    o != null && !string.IsNullOrEmpty(AssetDatabase.GetAssetPath(o));

Try / catch

try { EditorBuildSettings.AddConfigObject(name, obj, false); }
catch (Exception ex) when (ex.Message.Contains("non-persisted"))
{ AssetDatabase.CreateAsset(obj, path); AssetDatabase.SaveAssets(); /* retry */ }

Prevention

When it happens

Trigger: Calling AddConfigObject(name, obj, overwrite) where obj is an in-memory object (e.g. new ScriptableObject instance not saved as an asset, a runtime GameObject, a component on an unsaved scene) rather than an AssetDatabase-tracked asset.

Common situations: Creating a ScriptableObject with ScriptableObject.CreateInstance<T>() and passing it directly without AssetDatabase.CreateAsset; passing a prefab instance from an open scene rather than the prefab asset.

Related errors


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