Unity-Technologies/UnityCsReference · error · InvalidOperationException

A mode named {0} already exists in section {1}

Error message

A mode named {0} already exists in section {1}

What it means

Thrown by SceneView.AddCameraMode as an InvalidOperationException when the newly constructed CameraMode already exists in the static userDefinedModes list. Duplicate mode registration (same name and section) is not allowed because the dropdown would show ambiguous entries and the lookup logic assumes uniqueness.

Source

Thrown at Editor/Mono/SceneView/SceneView.cs:4640

            // Let's not persist the vertex snapping mode on 2D/3D mode change
            HandleUtility.ignoreRaySnapObjects = null;
            Tools.vertexDragging = false;
            Tools.handleOffset = Vector3.zero;

            var gridSettings = GridSettings.instance;
            gridSettings.rotation = Quaternion.identity;
            gridSettings.position = Vector3.zero;
        }

        public static CameraMode AddCameraMode(string name, string section)
        {
            if (string.IsNullOrEmpty(name))
                throw new ArgumentException("Cannot be null or empty", "name");
            if (string.IsNullOrEmpty(section))
                throw new ArgumentException("Cannot be null or empty", "section");
            var newMode = new CameraMode(DrawCameraMode.UserDefined, name, section);
            if (userDefinedModes.Contains(newMode))
                throw new InvalidOperationException(string.Format("A mode named {0} already exists in section {1}", name, section));
            userDefinedModes.Add(newMode);
            return newMode;
        }

        private static bool IsValidCameraMode(CameraMode cameraMode)
        {
            foreach (var mode in Enum.GetValues(typeof(DrawCameraMode)))
            {
                if (SceneRenderModeWindow.DrawCameraModeExists((DrawCameraMode)mode) && cameraMode == GetBuiltinCameraMode((DrawCameraMode)mode))
                {
                    return true;
                }
            }

            foreach (var tempCameraMode in userDefinedModes)
            {
                if (tempCameraMode == cameraMode)
                {

View on GitHub (pinned to 225b0fbdb5)

Solutions

  1. Check userDefinedModes (or use a helper) for an existing mode with the same name and section before adding.
  2. Guard registration with [InitializeOnLoad] deduplication logic.
  3. Use a unique name/section namespace prefix for your package's modes.

Example fix

// before
SceneView.AddCameraMode("MyMode", "Custom");

// after
var existing = SceneView.userDefinedModes.FirstOrDefault(m => m.name == "MyMode" && m.section == "Custom");
if (existing == null)
    SceneView.AddCameraMode("MyMode", "Custom");
Defensive patterns

Strategy: validation

Validate before calling

bool ModeExists(string name, string section)
{
    return SceneView.userDefinedModes.Any(m => m.name == name && m.section == section);
}

Try / catch

try
{
    SceneView.AddCameraMode(name, section);
}
catch (InvalidOperationException)
{
    // Mode already registered; retrieve existing instead.
}

Prevention

When it happens

Trigger: Calling AddCameraMode with the same name and section twice. Editor initialization code that runs on every domain reload re-registering modes without checking for existence. Multiple packages each registering the same mode name.

Common situations: InitializeOnLoad method that registers modes on every script recompilation. Two independent editor extensions using the same mode name in the same section.

Related errors


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