Unity-Technologies/UnityCsReference · error · ArgumentException

SetCodeEditor: Does not allow null editorPath

Error message

SetCodeEditor: Does not allow null editorPath

What it means

ArgumentException thrown by CodeEditor.SetCodeEditor when editorPath is null. SetCodeEditor persists the path to EditorPrefs ('kScriptsDefaultApp'), computes the current editor, and initializes it; a null path would corrupt preferences and is rejected. This is the canonical setter for the external script editor preference.

Source

Thrown at Editor/Mono/CodeEditor/CodeEditor.cs:189

        private static void AddIfPathExists(string name, string path, Dictionary<string, string> list)
        {
            if (list.ContainsKey(path)) return;
            if (Directory.Exists(path)) list.Add(path, name);
            else if (File.Exists(path)) list.Add(path, name);
        }

        [System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)]
        public static void SetExternalScriptEditor(string path)
        {
            Editor.SetCodeEditor(path);
        }

        public void SetCodeEditor(string editorPath)
        {
            if (editorPath == null)
            {
                throw new ArgumentException("SetCodeEditor: Does not allow null editorPath");
            }

            EditorPrefs.SetString("kScriptsDefaultApp", editorPath);
            m_CurrentEditor = ComputeCurrentEditor(editorPath);
            m_CurrentInstallation = GetInstallationForPath(editorPath);
            m_CurrentEditor.Initialize(editorPath);
        }

        private IExternalCodeEditor ComputeCurrentEditor(string editorPath)
        {
            if (m_ExternalCodeEditors.Count == 0)
            {
                TypeCollection collection = TypeCache.GetTypesDerivedFrom<IExternalCodeEditor>();
                for (int i = 0; i < collection.Count; i++)
                {
                    var codeEditorType = collection[i];
                    if (codeEditorType == typeof(DefaultExternalCodeEditor))
                        continue;

View on GitHub (pinned to 225b0fbdb5)

Solutions

  1. Validate the path is non-null and points to an existing editor executable before calling SetCodeEditor.
  2. Default to CodeEditor.SystemDefaultPath when no explicit editor is selected.
  3. In UI flows, disable the apply action until a valid path is present.

Example fix

// before
CodeEditor.SetCodeEditor(detectedPath);

// after
if (!string.IsNullOrEmpty(detectedPath))
    CodeEditor.SetCodeEditor(detectedPath);
else
    CodeEditor.SetCodeEditor(CodeEditor.SystemDefaultPath);
Defensive patterns

Strategy: validation

Validate before calling

if (string.IsNullOrEmpty(editorPath)) editorPath = CodeEditor.SystemDefaultPath;
CodeEditor.SetCodeEditor(editorPath);

Prevention

When it happens

Trigger: Calling CodeEditor.SetCodeEditor(null), often indirectly via Editor.SetCodeEditor(path) or SetExternalScriptEditor(path) when path is null. Happens when a settings UI commits a null/empty selection or a detection routine yields null.

Common situations: Preferences window saving before a path is chosen; automation that resets the script editor to a detected path that returned null; cross-platform code where the path detection fails on a given OS.

Related errors


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