Unity-Technologies/UnityCsReference · error · ArgumentException

Non-Component type must use ObjectFactory.CreateInstance ins

Error message

Non-Component type must use ObjectFactory.CreateInstance instead : {type.FullName}

What it means

Thrown by ObjectFactory.AddComponent when !type.IsSubclassOf(typeof(Component)) — the type is not a Component subclass. AddComponent only accepts Component-derived types; non-components (e.g., ScriptableObject, plain classes) must use CreateInstance.

Source

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

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

        public static GameObject CreateGameObject(Scene scene, HideFlags hideFlags, string name, params Type[] types)
        {
            if (!scene.IsValid())
            {
                throw new ArgumentException("Cannot create a GameObject to a null Scene.");
            }

            //Internally, this sets the target scene that the scene points to.
            EditorSceneManager.SetTargetSceneForNewGameObjects(scene);
            var go = CreateGameObject(name, types);
            go.hideFlags = hideFlags;
            EditorSceneManager.ClearTargetSceneForNewGameObjects();
            return go;
        }

View on GitHub (pinned to 225b0fbdb5)

Solutions

  1. Use ObjectFactory.CreateInstance for ScriptableObject / non-component types.
  2. Validate type.IsSubclassOf(typeof(Component)) before AddComponent.

Example fix

// before
ObjectFactory.AddComponent(gameObject, typeof(MyScriptableObject));
// after
if (type.IsSubclassOf(typeof(Component)))
    ObjectFactory.AddComponent(gameObject, type);
else
    ObjectFactory.CreateInstance(type);
Defensive patterns

Strategy: validation

Validate before calling

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

Type guard

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

Prevention

When it happens

Trigger: Calling ObjectFactory.AddComponent(gameObject, type) where type does not derive from Component.

Common situations: Generic 'add to object' helper that accepts arbitrary Types; confusing ScriptableObject with MonoBehaviour.

Related errors


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