Unity-Technologies/UnityCsReference · error · InvalidOperationException
EditorApplication.OpenScene() cannot be called when in the U
Error message
EditorApplication.OpenScene() cannot be called when in the Unity Editor is in play mode.
What it means
Thrown by the obsolete EditorApplication.OpenScene when EditorApplication.isPlaying is true at call time. The method is marked [Obsolete] (use EditorSceneManager.OpenScene); the play-mode guard is intentional because loading a scene while playing discards runtime state. InvalidOperationException reflects an invalid state for the operation, not a missing resource.
Source
Thrown at Editor/Mono/EditorApplication.deprecated.cs:39
[Obsolete("Use EditorSceneManager.NewScene (NewSceneSetup.EmptyScene)")]
public static void NewEmptyScene()
{
EditorSceneManager.NewScene(NewSceneSetup.EmptyScene);
}
// Opens the scene at /path/.
[Obsolete("Use EditorSceneManager.OpenScene")]
public static bool OpenScene(string path)
{
// Check that we're not in play mode first before opening the scene.
if (!isPlaying)
{
Scene scene = EditorSceneManager.OpenScene(path);
return scene.IsValid();
}
else
{
throw new InvalidOperationException(
"EditorApplication.OpenScene() cannot be called when in the Unity Editor is in play mode.");
}
}
[Obsolete("Use EditorSceneManager.OpenScene")]
public static void OpenSceneAdditive(string path)
{
// Case 712517:
// Behaviour change introduced in 5.3
// Previously we allowed OpenSceneAdditive to be called during playmode.
// This is no longer allowed, if it happens we exit playmode which is
// consistent with EditorSceneManager.OpenScene(path, OpenSceneMode.Additive)
if (Application.isPlaying)
{
Debug.LogWarning("Exiting playmode.\n" +
"OpenSceneAdditive was called at a point where there was no active scene.\n" +
"This usually means it was called in a PostprocessScene function during scene loading or it was called during playmode.\n" +View on GitHub (pinned to 225b0fbdb5)
Solutions
- Migrate to EditorSceneManager.OpenScene, which exits play mode consistently instead of throwing.
- If you must use the deprecated API, stop play mode first: EditorApplication.isPlaying = false; then open.
- Guard with: if (EditorApplication.isPlaying) EditorApplication.ExitPlaymode(); before opening.
Example fix
// before EditorApplication.OpenScene(scenePath); // after if (EditorApplication.isPlaying) EditorApplication.ExitPlaymode(); EditorSceneManager.OpenScene(scenePath);
Defensive patterns
Strategy: validation
Validate before calling
if (EditorApplication.isPlaying) EditorApplication.ExitPlaymode(); EditorSceneManager.OpenScene(path);
Prevention
- Stop using the deprecated EditorApplication.OpenScene; migrate to EditorSceneManager.
- Stop play mode before any scene-loading automation.
- Gate scene-load scripts on !isPlaying when they must run during play.
When it happens
Trigger: Calling EditorApplication.OpenScene(path) — the deprecated API — from code that runs while the editor is in play mode (an editor script triggered from a play-mode context, a runtime-invoked menu item, etc.).
Common situations: Legacy scripts or tutorials still calling EditorApplication.OpenScene; automation that runs without first checking/stopping play mode; ported code from pre-5.3 Unity.
Related errors
- Check the connected players list instead
- Parameter enumValue must be of type System.Enum
- CompileCSharp is no longer supported. Use UnityEditor.Compil
- AA is not supported on GUIViews
- GlobalObjectId.GetGlobalObjectIdSlow(int[], [Out] GlobalObje
AI-assisted analysis of Unity-Technologies/UnityCsReference@225b0fbdb5 (2026-08-13).
Data as JSON: /api/errors/51694c672475efd1.
Report an issue: GitHub.