{"record":{"id":"1e28865c5bd18037","repo":"Unity-Technologies/UnityCsReference","slug":"non-component-type-must-use-objectfactory-createin","errorCode":null,"errorMessage":"Non-Component type must use ObjectFactory.CreateInstance instead : {type.FullName}","messagePattern":"Non-Component type must use ObjectFactory\\.CreateInstance instead : (.+?)","errorType":"validation","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"Editor/Mono/ObjectFactory.bindings.cs","lineNumber":93,"sourceCode":"            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)))\n            {\n                throw new ArgumentException(\"Non-Component type must use ObjectFactory.CreateInstance instead : \" + type.FullName);\n            }\n            return AddDefaultComponent(gameObject, type);\n        }\n\n        public static GameObject CreateGameObject(Scene scene, HideFlags hideFlags, string name, params Type[] types)\n        {\n            if (!scene.IsValid())\n            {\n                throw new ArgumentException(\"Cannot create a GameObject to a null Scene.\");\n            }\n\n            //Internally, this sets the target scene that the scene points to.\n            EditorSceneManager.SetTargetSceneForNewGameObjects(scene);\n            var go = CreateGameObject(name, types);\n            go.hideFlags = hideFlags;\n            EditorSceneManager.ClearTargetSceneForNewGameObjects();\n            return go;\n        }","sourceCodeStart":75,"sourceCodeEnd":111,"githubUrl":"https://github.com/Unity-Technologies/UnityCsReference/blob/225b0fbdb57cc17d094e8056b71f8314aba56f73/Editor/Mono/ObjectFactory.bindings.cs#L75-L111","documentation":"Thrown by ObjectFactory.AddComponent when !type.IsSubclassOf(typeof(Component)) — the type is not a Component subclass. AddComponent only accepts Component-derived types; non-components (e.g., ScriptableObject, plain classes) must use CreateInstance.","triggerScenarios":"Calling ObjectFactory.AddComponent(gameObject, type) where type does not derive from Component.","commonSituations":"Generic 'add to object' helper that accepts arbitrary Types; confusing ScriptableObject with MonoBehaviour.","solutions":["Use ObjectFactory.CreateInstance for ScriptableObject / non-component types.","Validate type.IsSubclassOf(typeof(Component)) before AddComponent."],"exampleFix":"// before\nObjectFactory.AddComponent(gameObject, typeof(MyScriptableObject));\n// after\nif (type.IsSubclassOf(typeof(Component)))\n    ObjectFactory.AddComponent(gameObject, type);\nelse\n    ObjectFactory.CreateInstance(type);","handlingStrategy":"validation","validationCode":"if (type.IsSubclassOf(typeof(Component)))\n    ObjectFactory.AddComponent(gameObject, type);\nelse\n    ObjectFactory.CreateInstance(type);","typeGuard":"static bool IsComponentType(Type t) => t.IsSubclassOf(typeof(Component));","tryCatchPattern":null,"preventionTips":["Validate IsSubclassOf(typeof(Component)) before AddComponent.","Use CreateInstance for ScriptableObject types."],"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"}