{"record":{"id":"d77770345f331237","repo":"Unity-Technologies/UnityCsReference","slug":"component-type-must-be-created-using-objectfactory","errorCode":null,"errorMessage":"Component type must be created using ObjectFactory.AddComponent instead : {type.FullName}","messagePattern":"Component type must be created using ObjectFactory\\.AddComponent instead : (.+?)","errorType":"validation","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"Editor/Mono/ObjectFactory.bindings.cs","lineNumber":73,"sourceCode":"                throw new ArgumentException(\"The type \" + type.FullName + \" is not supported by the ObjectFactory.\");\n            }\n        }\n\n        public static T CreateInstance<T>() where T : Object\n        {\n            return (T)CreateInstance(typeof(T));\n        }\n\n        public static Object CreateInstance(Type type)\n        {\n            CheckTypeValidity(type);\n            if (type == typeof(GameObject))\n            {\n                throw new ArgumentException(\"GameObject type must be created using ObjectFactory.CreateGameObject instead : \" + type.FullName);\n            }\n            if (type.IsSubclassOf(typeof(Component)))\n            {\n                throw new ArgumentException(\"Component type must be created using ObjectFactory.AddComponent instead : \" + type.FullName);\n            }\n            if (type.GetConstructor(BindingFlags.NonPublic | BindingFlags.Instance, null, Type.EmptyTypes, null) != null)\n            {\n                throw new ArgumentException(type.FullName + \" constructor is not accessible which prevent this type from being used in ObjectFactory.\");\n            }\n            var obj = CreateDefaultInstance(type);\n            return obj;\n        }\n\n        public static T AddComponent<T>(GameObject gameObject) where T : Component\n        {\n            return (T)AddComponent(gameObject, typeof(T));\n        }\n\n        public static Component AddComponent(GameObject gameObject, Type type)\n        {\n            CheckTypeValidity(type);\n            if (!type.IsSubclassOf(typeof(Component)))","sourceCodeStart":55,"sourceCodeEnd":91,"githubUrl":"https://github.com/Unity-Technologies/UnityCsReference/blob/225b0fbdb57cc17d094e8056b71f8314aba56f73/Editor/Mono/ObjectFactory.bindings.cs#L55-L91","documentation":"Thrown by ObjectFactory.CreateInstance when type.IsSubclassOf(typeof(Component)) is true. Components must be added to a GameObject via ObjectFactory.AddComponent, not instantiated standalone, because they require a host GameObject.","triggerScenarios":"Calling ObjectFactory.CreateInstance on a Component subclass (MonoBehaviour).","commonSituations":"Generic instantiation helper that does not distinguish ScriptableObject-derived types from Component-derived types.","solutions":["Use ObjectFactory.AddComponent(gameObject, type) for components.","Branch on type.IsSubclassOf(typeof(Component)) in generic code."],"exampleFix":"// before\nObjectFactory.CreateInstance(typeof(MyMonoBehaviour));\n// after\nObjectFactory.AddComponent<MyMonoBehaviour>(gameObject);","handlingStrategy":"validation","validationCode":"if (type.IsSubclassOf(typeof(Component)))\n    ObjectFactory.AddComponent(go, type);\nelse\n    ObjectFactory.CreateInstance(type);","typeGuard":"static bool IsComponentType(Type t) => t.IsSubclassOf(typeof(Component));","tryCatchPattern":null,"preventionTips":["Distinguish Component-derived from ScriptableObject-derived types.","Route components through AddComponent."],"tags":["unity","object-factory","component","validation"],"backgroundTag":null,"analyzedSha":"225b0fbdb57cc17d094e8056b71f8314aba56f73","analyzedAt":"2026-08-13T19:07:19.849Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}