Unity-Technologies/UnityCsReference · error · ArgumentException

Cannot create a GameObject to a null Scene.

Error message

Cannot create a GameObject to a null Scene.

What it means

Thrown by ObjectFactory.CreateGameObject(Scene, HideFlags, string, params Type[]) when scene.IsValid() is false. A GameObject must be placed into a valid scene; a null/uninitialized Scene is rejected.

Source

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

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

        public static GameObject CreateGameObject(string name, params Type[] types)
        {
            var go = CreateDefaultGameObject(name);
            go.SetActive(false);
            foreach (var type in types)
            {
                AddComponent(go, type);
            }

View on GitHub (pinned to 225b0fbdb5)

Solutions

  1. Ensure the scene is open and valid: check scene.IsValid() and scene.isLoaded before calling.
  2. Use the CreateGameObject(string, params Type[]) overload to add to the active scene instead.
  3. Wait for SceneManager sceneLoaded callback if loading additively.

Example fix

// before
var go = ObjectFactory.CreateGameObject(default(Scene), HideFlags.None, "foo");
// after
var scene = EditorSceneManager.NewScene(NewSceneSetup.EmptyScene, NewSceneMode.Additive);
if (scene.IsValid())
    var go = ObjectFactory.CreateGameObject(scene, HideFlags.None, "foo");
Defensive patterns

Strategy: validation

Validate before calling

if (scene.IsValid())
    var go = ObjectFactory.CreateGameObject(scene, hideFlags, name, types);

Type guard

static bool IsUsableScene(Scene s) => s.IsValid() && s.isLoaded;

Prevention

When it happens

Trigger: Calling the Scene-overload of CreateGameObject with a Scene that is not valid (default/uninitialized or an unopened scene handle).

Common situations: Passing default(Scene); passing a Scene captured before it was opened; additive scene load not yet completed when CreateGameObject is called.

Related errors


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