Unity-Technologies/UnityCsReference · error · ArgumentException

Component type must be created using ObjectFactory.AddCompon

Error message

Component type must be created using ObjectFactory.AddComponent instead : {type.FullName}

What it means

Thrown by ObjectFactory.CreateInstance when type.IsSubclassOf(typeof(Component)) is true. Components must be added to a GameObject via ObjectFactory.AddComponent, not instantiated standalone, because they require a host GameObject.

Source

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

                throw new ArgumentException("The type " + type.FullName + " is not supported by the ObjectFactory.");
            }
        }

        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)))

View on GitHub (pinned to 225b0fbdb5)

Solutions

  1. Use ObjectFactory.AddComponent(gameObject, type) for components.
  2. Branch on type.IsSubclassOf(typeof(Component)) in generic code.

Example fix

// before
ObjectFactory.CreateInstance(typeof(MyMonoBehaviour));
// after
ObjectFactory.AddComponent<MyMonoBehaviour>(gameObject);
Defensive patterns

Strategy: validation

Validate before calling

if (type.IsSubclassOf(typeof(Component)))
    ObjectFactory.AddComponent(go, type);
else
    ObjectFactory.CreateInstance(type);

Type guard

static bool IsComponentType(Type t) => t.IsSubclassOf(typeof(Component));

Prevention

When it happens

Trigger: Calling ObjectFactory.CreateInstance on a Component subclass (MonoBehaviour).

Common situations: Generic instantiation helper that does not distinguish ScriptableObject-derived types from Component-derived types.

Related errors


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