{"record":{"id":"167c566c27240245","repo":"Unity-Technologies/UnityCsReference","slug":"abstract-types-can-t-be-used-in-the-objectfactory","errorCode":null,"errorMessage":"Abstract types can't be used in the ObjectFactory : {type.FullName}","messagePattern":"Abstract types can't be used in the ObjectFactory : (.+?)","errorType":"validation","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"Editor/Mono/ObjectFactory.bindings.cs","lineNumber":51,"sourceCode":"\n        [FreeFunction]\n        static extern GameObject CreateDefaultGameObject(string name);\n\n        [AutoStaticsCleanupOnCodeReload]\n        public static event Action<Component> componentWasAdded;\n\n        [RequiredByNativeCode]\n        static void InvokeComponentWasAdded(Component component)\n        {\n            if (componentWasAdded != null)\n                componentWasAdded(component);\n        }\n\n        static void CheckTypeValidity(Type type)\n        {\n            if (type.IsAbstract)\n            {\n                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);","sourceCodeStart":33,"sourceCodeEnd":69,"githubUrl":"https://github.com/Unity-Technologies/UnityCsReference/blob/225b0fbdb57cc17d094e8056b71f8314aba56f73/Editor/Mono/ObjectFactory.bindings.cs#L33-L69","documentation":"Thrown by ObjectFactory.CheckTypeValidity when type.IsAbstract is true. Abstract types cannot be instantiated, so ObjectFactory refuses to create them.","triggerScenarios":"Calling ObjectFactory.CreateInstance with an abstract Type (e.g., typeof(ScriptableObject) itself, or an abstract MonoBehaviour subclass).","commonSituations":"Generic factory code passing a runtime Type that resolves to an abstract base; reflection picking the base instead of a concrete subclass.","solutions":["Pass a concrete (non-abstract) Type to CreateInstance.","Resolve the abstract type to a concrete subclass before invoking.","Guard with !type.IsAbstract before calling."],"exampleFix":"// before\nObjectFactory.CreateInstance(typeof(MyAbstractBase));\n// after\nObjectFactory.CreateInstance(typeof(MyConcreteDerived));","handlingStrategy":"validation","validationCode":"if (!type.IsAbstract) ObjectFactory.CreateInstance(type);","typeGuard":"static bool IsInstantiable(Type t) => !t.IsAbstract && !t.IsInterface;","tryCatchPattern":null,"preventionTips":["Resolve abstract types to concrete subclasses.","Guard IsAbstract in generic factory code."],"tags":["unity","object-factory","reflection","validation"],"backgroundTag":null,"analyzedSha":"225b0fbdb57cc17d094e8056b71f8314aba56f73","analyzedAt":"2026-08-13T19:07:19.849Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}