{"record":{"id":"b76c7a8323711734","repo":"Unity-Technologies/UnityCsReference","slug":"cannot-create-a-gameobject-to-a-null-scene","errorCode":null,"errorMessage":"Cannot create a GameObject to a null Scene.","messagePattern":"Cannot create a GameObject to a null Scene\\.","errorType":"validation","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"Editor/Mono/ObjectFactory.bindings.cs","lineNumber":102,"sourceCode":"        {\n            return (T)AddComponent(gameObject, typeof(T));\n        }\n\n        public static Component AddComponent(GameObject gameObject, Type type)\n        {\n            CheckTypeValidity(type);\n            if (!type.IsSubclassOf(typeof(Component)))\n            {\n                throw new ArgumentException(\"Non-Component type must use ObjectFactory.CreateInstance instead : \" + type.FullName);\n            }\n            return AddDefaultComponent(gameObject, type);\n        }\n\n        public static GameObject CreateGameObject(Scene scene, HideFlags hideFlags, string name, params Type[] types)\n        {\n            if (!scene.IsValid())\n            {\n                throw new ArgumentException(\"Cannot create a GameObject to a null Scene.\");\n            }\n\n            //Internally, this sets the target scene that the scene points to.\n            EditorSceneManager.SetTargetSceneForNewGameObjects(scene);\n            var go = CreateGameObject(name, types);\n            go.hideFlags = hideFlags;\n            EditorSceneManager.ClearTargetSceneForNewGameObjects();\n            return go;\n        }\n\n        public static GameObject CreateGameObject(string name, params Type[] types)\n        {\n            var go = CreateDefaultGameObject(name);\n            go.SetActive(false);\n            foreach (var type in types)\n            {\n                AddComponent(go, type);\n            }","sourceCodeStart":84,"sourceCodeEnd":120,"githubUrl":"https://github.com/Unity-Technologies/UnityCsReference/blob/225b0fbdb57cc17d094e8056b71f8314aba56f73/Editor/Mono/ObjectFactory.bindings.cs#L84-L120","documentation":"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.","triggerScenarios":"Calling the Scene-overload of CreateGameObject with a Scene that is not valid (default/uninitialized or an unopened scene handle).","commonSituations":"Passing default(Scene); passing a Scene captured before it was opened; additive scene load not yet completed when CreateGameObject is called.","solutions":["Ensure the scene is open and valid: check scene.IsValid() and scene.isLoaded before calling.","Use the CreateGameObject(string, params Type[]) overload to add to the active scene instead.","Wait for SceneManager sceneLoaded callback if loading additively."],"exampleFix":"// before\nvar go = ObjectFactory.CreateGameObject(default(Scene), HideFlags.None, \"foo\");\n// after\nvar scene = EditorSceneManager.NewScene(NewSceneSetup.EmptyScene, NewSceneMode.Additive);\nif (scene.IsValid())\n    var go = ObjectFactory.CreateGameObject(scene, HideFlags.None, \"foo\");","handlingStrategy":"validation","validationCode":"if (scene.IsValid())\n    var go = ObjectFactory.CreateGameObject(scene, hideFlags, name, types);","typeGuard":"static bool IsUsableScene(Scene s) => s.IsValid() && s.isLoaded;","tryCatchPattern":null,"preventionTips":["Check scene.IsValid() and isLoaded before use.","Prefer the active-scene overload when unsure.","Wait for sceneLoaded in additive flows."],"tags":["unity","object-factory","scene","gameobject","validation"],"backgroundTag":null,"analyzedSha":"225b0fbdb57cc17d094e8056b71f8314aba56f73","analyzedAt":"2026-08-13T19:07:19.849Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}