Unity-Technologies/UnityCsReference · error · ArgumentException

Can't connect a Prefab in the StreamingAssets folder to…

Error message

Can't connect a Prefab in the StreamingAssets folder to GameObjects in the scene. To save a Prefab to the StreamingAssets folder use SaveAsPrefabAsset instead.

What it means

Thrown by SaveAsPrefabAssetAndConnect when the target assetPath is inside the StreamingAssets folder. StreamingAssets are shipped read-only and cannot be connected to scene instances; connecting requires a standard asset folder. Unity directs the developer to use SaveAsPrefabAsset (without connect) for that location.

Solutions

  1. Use SaveAsPrefabAsset (no connect) instead of SaveAsPrefabAssetAndConnect for StreamingAssets paths.
  2. If you need a connectable Prefab, save it under Assets/ and copy to StreamingAssets separately if runtime streaming is required.
  3. Check IsPathInStreamingAssets(assetPath) before deciding which API to call.

Example fix

// before
PrefabUtility.SaveAsPrefabAssetAndConnect(go,
    "Assets/StreamingAssets/x.prefab", InteractionMode.AutomatedAction);
// after
PrefabUtility.SaveAsPrefabAsset(go,
    "Assets/StreamingAssets/x.prefab");
Defensive patterns

Strategy: validation

Validate before calling

void SavePrefabSmart(GameObject go, string path, bool connect)
{
    bool inStreaming = path.Contains("StreamingAssets");
    if (connect && inStreaming)
    {
        Debug.LogWarning("StreamingAssets path; using SaveAsPrefabAsset.");
        PrefabUtility.SaveAsPrefabAsset(go, path);
    }
    else if (connect)
        PrefabUtility.SaveAsPrefabAssetAndConnect(go, path, InteractionMode.AutomatedAction);
    else
        PrefabUtility.SaveAsPrefabAsset(go, path);
}

Type guard

static bool IsConnectablePath(string path) =>
    !string.IsNullOrEmpty(path) && !path.Contains("StreamingAssets");

Prevention

When it happens

Trigger: Calling SaveAsPrefabAssetAndConnect with an assetPath that begins under the project's StreamingAssets directory (IsPathInStreamingAssets returns true).

Common situations: Developer wants to store a Prefab in StreamingAssets for runtime streaming and inadvertently uses the connect variant. Confusion about which StreamingAssets restrictions apply.

Related errors


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

Appendix: source

Thrown at Editor/Mono/Prefabs/PrefabUtility.cs:2131

        public static GameObject SaveAsPrefabAsset(GameObject instanceRoot, string assetPath)
        {
            bool success;
            return SaveAsPrefabAsset(instanceRoot, assetPath, out success);
        }

        public static GameObject SaveAsPrefabAssetAndConnect(GameObject instanceRoot, string assetPath, InteractionMode action)
        {
            bool success;
            return SaveAsPrefabAssetAndConnect(instanceRoot, assetPath, action, out success);
        }

        public static GameObject SaveAsPrefabAssetAndConnect(GameObject instanceRoot, string assetPath, InteractionMode action, out bool success)
        {
            SaveAsPrefabAssetArgumentCheck(instanceRoot, assetPath, true);

            if (IsPathInStreamingAssets(assetPath))
                throw new ArgumentException("Can't connect a Prefab in the StreamingAssets folder to GameObjects in the scene. To save a Prefab to the StreamingAssets folder use SaveAsPrefabAsset instead.");

            var actionName = "Connect to Prefab";

            if (action == InteractionMode.UserAction)
            {
                Undo.RegisterFullObjectHierarchyUndo(instanceRoot, actionName);
            }

            var assetRoot = SaveAsPrefabAssetAndConnect_Internal(instanceRoot, assetPath, out success);

            if (!success)
            {
                return null;
            }

            if (action == InteractionMode.UserAction)
            {
                Undo.RecordCreatedObject(GetPrefabInstanceHandle(instanceRoot), actionName);

View on GitHub (pinned to 225b0fbdb5)