{"record":{"id":"c6ecf8f60db546eb","repo":"Unity-Technologies/UnityCsReference","slug":"type-fullname-constructor-is-not-accessible-whic","errorCode":null,"errorMessage":"{type.FullName} constructor is not accessible which prevent this type from being used in ObjectFactory.","messagePattern":"(.+?) constructor is not accessible which prevent this type from being used in ObjectFactory\\.","errorType":"validation","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"Editor/Mono/ObjectFactory.bindings.cs","lineNumber":77,"sourceCode":"        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)))\n            {\n                throw new ArgumentException(\"Non-Component type must use ObjectFactory.CreateInstance instead : \" + type.FullName);\n            }\n            return AddDefaultComponent(gameObject, type);","sourceCodeStart":59,"sourceCodeEnd":95,"githubUrl":"https://github.com/Unity-Technologies/UnityCsReference/blob/225b0fbdb57cc17d094e8056b71f8314aba56f73/Editor/Mono/ObjectFactory.bindings.cs#L59-L95","documentation":"Thrown by ObjectFactory.CreateInstance when the type has only a non-public instance constructor with no parameters — i.e., the factory cannot instantiate it because the required default ctor is inaccessible. Note: the guard checks for a NonPublic ctor and throws, enforcing that ObjectFactory-managed types expose an accessible parameterless constructor.","triggerScenarios":"Calling CreateInstance on a type whose only parameterless constructor is private/protected/internal (NonPublic | Instance).","commonSituations":"Singleton pattern with private ctor; types designed for deserialization-only; nested types with restricted construction.","solutions":["Add a public parameterless constructor to the type.","If the restriction is intentional, create the instance through the type's intended factory method instead of ObjectFactory."],"exampleFix":"// before\nclass Foo { private Foo() {} } // only non-public ctor\n// after\nclass Foo { public Foo() {} }","handlingStrategy":"validation","validationCode":"var ctor = type.GetConstructor(BindingFlags.Public|BindingFlags.Instance, null, Type.EmptyTypes, null);\nif (ctor != null) ObjectFactory.CreateInstance(type);","typeGuard":"static bool HasPublicParameterlessCtor(Type t) =>\n    t.GetConstructor(BindingFlags.Public|BindingFlags.Instance, null, Type.EmptyTypes, null) != null;","tryCatchPattern":null,"preventionTips":["Expose a public parameterless constructor for factory types.","Avoid singleton-private-ctor types with ObjectFactory."],"tags":["unity","object-factory","reflection","constructor"],"backgroundTag":null,"analyzedSha":"225b0fbdb57cc17d094e8056b71f8314aba56f73","analyzedAt":"2026-08-13T19:07:19.849Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}