Unity-Technologies/UnityCsReference · error · ArgumentException

{type.FullName} constructor is not accessible which prevent

Error message

{type.FullName} constructor is not accessible which prevent this type from being used in ObjectFactory.

What it means

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.

Source

Thrown at Editor/Mono/ObjectFactory.bindings.cs:77

        public static T CreateInstance<T>() where T : Object
        {
            return (T)CreateInstance(typeof(T));
        }

        public static Object CreateInstance(Type type)
        {
            CheckTypeValidity(type);
            if (type == typeof(GameObject))
            {
                throw new ArgumentException("GameObject type must be created using ObjectFactory.CreateGameObject instead : " + type.FullName);
            }
            if (type.IsSubclassOf(typeof(Component)))
            {
                throw new ArgumentException("Component type must be created using ObjectFactory.AddComponent instead : " + type.FullName);
            }
            if (type.GetConstructor(BindingFlags.NonPublic | BindingFlags.Instance, null, Type.EmptyTypes, null) != null)
            {
                throw new ArgumentException(type.FullName + " constructor is not accessible which prevent this type from being used in ObjectFactory.");
            }
            var obj = CreateDefaultInstance(type);
            return obj;
        }

        public static T AddComponent<T>(GameObject gameObject) where T : Component
        {
            return (T)AddComponent(gameObject, typeof(T));
        }

        public static Component AddComponent(GameObject gameObject, Type type)
        {
            CheckTypeValidity(type);
            if (!type.IsSubclassOf(typeof(Component)))
            {
                throw new ArgumentException("Non-Component type must use ObjectFactory.CreateInstance instead : " + type.FullName);
            }
            return AddDefaultComponent(gameObject, type);

View on GitHub (pinned to 225b0fbdb5)

Solutions

  1. Add a public parameterless constructor to the type.
  2. If the restriction is intentional, create the instance through the type's intended factory method instead of ObjectFactory.

Example fix

// before
class Foo { private Foo() {} } // only non-public ctor
// after
class Foo { public Foo() {} }
Defensive patterns

Strategy: validation

Validate before calling

var ctor = type.GetConstructor(BindingFlags.Public|BindingFlags.Instance, null, Type.EmptyTypes, null);
if (ctor != null) ObjectFactory.CreateInstance(type);

Type guard

static bool HasPublicParameterlessCtor(Type t) =>
    t.GetConstructor(BindingFlags.Public|BindingFlags.Instance, null, Type.EmptyTypes, null) != null;

Prevention

When it happens

Trigger: Calling CreateInstance on a type whose only parameterless constructor is private/protected/internal (NonPublic | Instance).

Common situations: Singleton pattern with private ctor; types designed for deserialization-only; nested types with restricted construction.

Related errors


AI-assisted analysis of Unity-Technologies/UnityCsReference@225b0fbdb5 (2026-08-13). Data as JSON: /api/errors/c6ecf8f60db546eb. Report an issue: GitHub.