{"record":{"id":"78953512605fddcf","repo":"Unity-Technologies/UnityCsReference","slug":"creating-a-variant-of-an-object-into-the-source-fi","errorCode":null,"errorMessage":"Creating a variant of an object into the source file of the input object is not allowed","messagePattern":"Creating a variant of an object into the source file of the input object is not allowed","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"Editor/Mono/Prefabs/PrefabUtility.bindings.cs","lineNumber":241,"sourceCode":"        extern public static GameObject FindPrefabRoot([NotNull] GameObject source);\n\n        internal static GameObject CreateVariant(GameObject assetRoot, string path)\n        {\n            if (assetRoot == null)\n                throw new ArgumentNullException(\"The inputObject is null\");\n\n            if (!IsPartOfPrefabAsset(assetRoot))\n                throw new ArgumentException(\"Given input object is not a prefab asset\");\n\n            if (assetRoot.transform.root.gameObject != assetRoot)\n                throw new ArgumentException(\"Object to create variant from has to be a Prefab root\");\n\n            if (path == null)\n                throw new ArgumentNullException(\"The path is null\");\n\n            var assetRootObjectPath = AssetDatabase.GetAssetPath(assetRoot);\n            if (Paths.AreEqual(path, assetRootObjectPath, true))\n                throw new ArgumentException(\"Creating a variant of an object into the source file of the input object is not allowed\");\n\n            if (!Paths.IsValidAssetPath(path, \".prefab\"))\n                throw new ArgumentException(\"Given path is not valid: '\" + path + \"'\");\n\n            return CreateVariant_Internal(assetRoot, path);\n        }\n\n        [NativeMethod(\"CreateVariant\", IsFreeFunction = true)]\n        extern private static GameObject CreateVariant_Internal([NotNull] GameObject original, string path);\n\n        [StaticAccessor(\"PrefabUtilityBindings\", StaticAccessorType.DoubleColon)]\n        [NativeMethod(ThrowsException = true)]\n        extern private static GameObject SavePrefab_Internal([NotNull] GameObject root, string path, bool connectToInstance, out bool success);\n\n        [StaticAccessor(\"PrefabUtilityBindings\", StaticAccessorType.DoubleColon)]\n        extern private static GameObject ApplyPrefabInstance_Internal([NotNull] GameObject root);\n\n        [StaticAccessor(\"PrefabUtilityBindings\", StaticAccessorType.DoubleColon)]","sourceCodeStart":223,"sourceCodeEnd":259,"githubUrl":"https://github.com/Unity-Technologies/UnityCsReference/blob/225b0fbdb57cc17d094e8056b71f8314aba56f73/Editor/Mono/Prefabs/PrefabUtility.bindings.cs#L223-L259","documentation":"Thrown by CreateVariant when the destination `path` resolves to the same file as the source prefab asset (case-insensitive compare via Paths.AreEqual). Creating a variant of a prefab into its own source file would produce a self-referential/corrupt asset, so Unity blocks it. The check runs only after path is confirmed non-null.","triggerScenarios":"Calling CreateVariant with a path equal to `AssetDatabase.GetAssetPath(assetRoot)`, or a path differing only by case. Reusing the original prefab's path verbatim, or generating the variant path from the source asset path without changing the filename.","commonSituations":"Right-click 'Create Variant' workflows where the user picked the same filename, or scripts that derive the output path from the input asset path without appending a suffix. Cross-platform case-insensitive filesystems making two apparently different paths collide.","solutions":["Choose a distinct output filename, e.g. append \"Variant\": AssetDatabase.GenerateUniqueAssetPath(\"Assets/Prefabs/\" + root.name + \"Variant.prefab\").","Verify the resolved path differs from AssetDatabase.GetAssetPath(assetRoot) before calling.","Prompt the user to pick a different folder/file if they reused the source name."],"exampleFix":"// before\nstring srcPath = AssetDatabase.GetAssetPath(root);\nvar v = PrefabUtility.CreateVariant(root, srcPath); // throws\n\n// after\nstring dir = System.IO.Path.GetDirectoryName(srcPath);\nstring newPath = AssetDatabase.GenerateUniqueAssetPath(\n    dir + \"/\" + root.name + \"Variant.prefab\");\nvar v = PrefabUtility.CreateVariant(root, newPath);","handlingStrategy":"validation","validationCode":"string srcPath = AssetDatabase.GetAssetPath(assetRoot);\nif (PathsAreEqual(path, srcPath))\n    path = AssetDatabase.GenerateUniqueAssetPath(\n        System.IO.Path.GetDirectoryName(srcPath).Replace('\\\\','/') + \"/\" + assetRoot.name + \"Variant.prefab\");","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always derive a new filename (append 'Variant' or a counter) from the source path.","Compare against AssetDatabase.GetAssetPath(assetRoot) case-insensitively before calling.","Use GenerateUniqueAssetPath so duplicates never collide with the source."],"tags":["prefab","create-variant","self-reference","asset-path"],"backgroundTag":null,"analyzedSha":"225b0fbdb57cc17d094e8056b71f8314aba56f73","analyzedAt":"2026-08-13T19:07:19.849Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}