Unity-Technologies/UnityCsReference · error · ArgumentException

GameObject type must be created using ObjectFactory.CreateGa

Error message

GameObject type must be created using ObjectFactory.CreateGameObject instead : {type.FullName}

What it means

Thrown by ObjectFactory.CreateInstance when type == typeof(GameObject). GameObjects must be created via CreateGameObject, not CreateInstance, because they need scene/transform initialization.

Source

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

                throw new ArgumentException("Abstract types can't be used in the ObjectFactory : " + type.FullName);
            }
            if (Attribute.GetCustomAttribute(type, typeof(ExcludeFromObjectFactoryAttribute)) != null)
            {
                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));
        }

View on GitHub (pinned to 225b0fbdb5)

Solutions

  1. Use ObjectFactory.CreateGameObject(...) instead.
  2. Special-case typeof(GameObject) in generic helpers.

Example fix

// before
ObjectFactory.CreateInstance(typeof(GameObject));
// after
ObjectFactory.CreateGameObject("New GameObject");
Defensive patterns

Strategy: validation

Validate before calling

if (type == typeof(GameObject))
    ObjectFactory.CreateGameObject("New");
else
    ObjectFactory.CreateInstance(type);

Type guard

static bool IsGameObject(Type t) => t == typeof(GameObject);

Prevention

When it happens

Trigger: Calling ObjectFactory.CreateInstance(typeof(GameObject)).

Common situations: Generic factory helper that routes all types through CreateInstance without special-casing GameObject.

Related errors


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