{"record":{"id":"c18dd24fdabf9aff","repo":"Unity-Technologies/UnityCsReference","slug":"the-component-typeof-t-name-already-exists-in-t","errorCode":null,"errorMessage":"The component {typeof(T).Name} already exists in the build profile {name}.","messagePattern":"The component (.+?) already exists in the build profile (.+?)\\.","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"Editor/Mono/BuildProfile/BuildProfile.API.cs","lineNumber":54,"sourceCode":"                return;\n\n            BuildProfileModuleUtil.SwitchLegacyActiveFromBuildProfile(buildProfile);\n        }\n\n        /// <summary>\n        /// Instantiates a new component of type T and adds it as a sub-asset to the build profile.\n        /// </summary>\n        /// <returns>Returns the newly created ScriptableObect.</returns>\n        /// <exception cref=\"ArgumentException\">\n        /// Thrown when a sub asset of the same type already exists in the build profile.\n        /// </exception>\n        [VisibleToOtherModules]\n        public T CreateComponent<T>() where T : UnityEngine.Object\n        {\n            var found = GetComponent<T>();\n            if (found != null)\n            {\n                throw new ArgumentException($\"The component {typeof(T).Name} already exists in the build profile {name}.\");\n            }\n\n            var type = typeof(T);\n            if (type.IsSubclassOf(typeof(ScriptableObject)))\n            {\n                var objectToAdd = ScriptableObject.CreateInstance(type);\n                objectToAdd.hideFlags = HideFlags.HideInHierarchy;\n                objectToAdd.name = type.Name;\n                AssetDatabase.AddObjectToAsset(objectToAdd, this);\n                return objectToAdd as T;\n            }\n\n            throw new ArgumentException($\"Type {type.Name} is not a ScriptableObject or a supported settings object type.\");\n        }\n\n        /// <summary>\n        /// Gets a component of type T associated with the build profile, its global fallback,\n        /// or null if the component is not available.","sourceCodeStart":36,"sourceCodeEnd":72,"githubUrl":"https://github.com/Unity-Technologies/UnityCsReference/blob/225b0fbdb57cc17d094e8056b71f8314aba56f73/Editor/Mono/BuildProfile/BuildProfile.API.cs#L36-L72","documentation":"Thrown by BuildProfile.CreateComponent<T>() when a sub-asset component of the same type T already exists on the build profile. Unity's BuildProfile enforces a one-component-per-type invariant, so attempting to create a duplicate is rejected with an ArgumentException. The check delegates to GetComponent<T>() first, and only creates a new ScriptableObject instance if none is found.","triggerScenarios":"Calling CreateComponent<T>() when GetComponent<T>() already returns a non-null instance for the same type T on the same BuildProfile. Common when initialization code runs twice or a component was created via AddComponent or deserialization previously.","commonSituations":"Re-running editor setup/initialization scripts that create components without checking existence first; migrating build profiles that already carry deserialized components; calling CreateComponent inside a loop or repeated editor callback.","solutions":["Call GetComponent<T>() first and only call CreateComponent<T>() when it returns null.","If you intend to replace, call RemoveComponent<T>() (or ForceRemoveComponent<T>() for required components) before CreateComponent<T>().","Audit the calling code to prevent the CreateComponent call from running more than once per profile (e.g. guard with a flag or existence check)."],"exampleFix":"// before\nprofile.CreateComponent<MySettings>();\n\n// after\nif (profile.GetComponent<MySettings>() == null)\n    profile.CreateComponent<MySettings>();","handlingStrategy":"validation","validationCode":"if (profile.GetComponent<T>() != null) { /* already exists; skip or replace */ }","typeGuard":"static bool HasComponent<P,T>(P profile) where P : BuildProfile where T : UnityEngine.Object => profile.GetComponent<T>() != null;","tryCatchPattern":null,"preventionTips":["Always call GetComponent<T>() before CreateComponent<T>().","Make profile initialization idempotent so it never runs twice.","Centralize component creation in one setup method."],"tags":["unity","buildprofile","duplicate","component","argumentexception"],"backgroundTag":null,"analyzedSha":"225b0fbdb57cc17d094e8056b71f8314aba56f73","analyzedAt":"2026-08-13T19:07:19.849Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}