{"record":{"id":"7ae67cef50b6862f","repo":"Unity-Technologies/UnityCsReference","slug":"gameobject-type-must-be-created-using-objectfactor","errorCode":null,"errorMessage":"GameObject type must be created using ObjectFactory.CreateGameObject instead : {type.FullName}","messagePattern":"GameObject type must be created using ObjectFactory\\.CreateGameObject instead : (.+?)","errorType":"validation","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"Editor/Mono/ObjectFactory.bindings.cs","lineNumber":69,"sourceCode":"                throw new ArgumentException(\"Abstract types can't be used in the ObjectFactory : \" + type.FullName);\n            }\n            if (Attribute.GetCustomAttribute(type, typeof(ExcludeFromObjectFactoryAttribute)) != null)\n            {\n                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","sourceCodeStart":51,"sourceCodeEnd":87,"githubUrl":"https://github.com/Unity-Technologies/UnityCsReference/blob/225b0fbdb57cc17d094e8056b71f8314aba56f73/Editor/Mono/ObjectFactory.bindings.cs#L51-L87","documentation":"Thrown by ObjectFactory.CreateInstance when type == typeof(GameObject). GameObjects must be created via CreateGameObject, not CreateInstance, because they need scene/transform initialization.","triggerScenarios":"Calling ObjectFactory.CreateInstance(typeof(GameObject)).","commonSituations":"Generic factory helper that routes all types through CreateInstance without special-casing GameObject.","solutions":["Use ObjectFactory.CreateGameObject(...) instead.","Special-case typeof(GameObject) in generic helpers."],"exampleFix":"// before\nObjectFactory.CreateInstance(typeof(GameObject));\n// after\nObjectFactory.CreateGameObject(\"New GameObject\");","handlingStrategy":"validation","validationCode":"if (type == typeof(GameObject))\n    ObjectFactory.CreateGameObject(\"New\");\nelse\n    ObjectFactory.CreateInstance(type);","typeGuard":"static bool IsGameObject(Type t) => t == typeof(GameObject);","tryCatchPattern":null,"preventionTips":["Special-case GameObject in generic helpers.","Use CreateGameObject consistently for GameObjects."],"tags":["unity","object-factory","gameobject","validation"],"backgroundTag":null,"analyzedSha":"225b0fbdb57cc17d094e8056b71f8314aba56f73","analyzedAt":"2026-08-13T19:07:19.849Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}