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
- 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.
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
- Check scene.IsValid() and isLoaded before use.
- Prefer the active-scene overload when unsure.
- Wait for sceneLoaded in additive flows.
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
- GameObject type must be created using ObjectFactory.CreateGa
- Abstract types can't be used in the ObjectFactory : {type.Fu
- The type {type.FullName} is not supported by the ObjectFacto
- Component type must be created using ObjectFactory.AddCompon
- Non-Component type must use ObjectFactory.CreateInstance ins
AI-assisted analysis of Unity-Technologies/UnityCsReference@225b0fbdb5 (2026-08-13).
Data as JSON: /api/errors/b76c7a8323711734.
Report an issue: GitHub.