Unity-Technologies/UnityCsReference · error · InvalidOperationException

Preview scene could not be created

Error message

Preview scene could not be created

What it means

Thrown by the PreviewScene constructor when EditorSceneManager.NewPreviewScene() returns an invalid Scene. This is an environment/resource failure: the editor could not allocate a preview scene.

Source

Thrown at Editor/Mono/Inspector/PreviewRenderUtility.cs:29

using UnityEngine.Experimental.Rendering;
using UnityEngine.SceneManagement;
using Unity.Scripting.LifecycleManagement;
using Debug = UnityEngine.Debug;
using Object = UnityEngine.Object;

namespace UnityEditor
{
    internal class PreviewScene : IDisposable
    {
        private readonly Scene m_Scene;
        private readonly List<GameObject> m_GameObjects = new List<GameObject>();
        private readonly Camera m_Camera;

        public PreviewScene(string sceneName)
        {
            m_Scene = EditorSceneManager.NewPreviewScene();
            if (!m_Scene.IsValid())
                throw new InvalidOperationException("Preview scene could not be created");

            m_Scene.name = sceneName;

            var camGO = EditorUtility.CreateGameObjectWithHideFlags("Preview Scene Camera", HideFlags.HideAndDontSave, typeof(Camera));
            AddGameObject(camGO);
            m_Camera = camGO.GetComponent<Camera>();
            camera.cameraType = CameraType.Preview;
            camera.enabled = false;
            camera.clearFlags = CameraClearFlags.Depth;
            camera.fieldOfView = 15;
            camera.farClipPlane = 10.0f;
            camera.nearClipPlane = 2.0f;

            // Explicitly use forward rendering for all previews
            // (deferred fails when generating some static previews at editor launch; and we never want
            // vertex lit previews if that is chosen in the player settings)
            camera.renderingPath = RenderingPath.Forward;
            camera.useOcclusionCulling = false;

View on GitHub (pinned to 225b0fbdb5)

Solutions

  1. Construct PreviewScene only on the main editor thread during normal editor state (not during play/domain reload).
  2. Reuse a single PreviewRenderUtility instance rather than creating many PreviewScenes.
  3. Guard by checking EditorApplication.isPlayingOrWillChangePlaymode / editor state before creating previews.

Example fix

// before
var scene = new PreviewScene("MyPreview");

// after
if (InternalEditorUtility.isEditorRunning && !EditorApplication.isPlayingOrWillChangePlaymode)
    var scene = new PreviewScene("MyPreview");
else
    Debug.LogWarning("Skipping preview: editor cannot create preview scene now.");
Defensive patterns

Strategy: validation

Validate before calling

bool ok = InternalEditorUtility.isEditorRunning && !EditorApplication.isPlayingOrWillChangePlaymode;

Try / catch

try { new PreviewScene("name"); }
catch (InvalidOperationException) { Debug.LogWarning("Preview scene unavailable"); }

Prevention

When it happens

Trigger: Constructing a PreviewScene (used by PreviewRenderUtility) when the editor cannot create a new preview scene, e.g. during domain reload, editor shutdown, or when too many preview scenes are already open.

Common situations: Editor resource exhaustion. Constructing preview scenes off the main thread or before the editor is fully initialized. Running in a batchmode/headless context where scene creation is unavailable.

Related errors


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