Unity-Technologies/UnityCsReference · error · InvalidOperationException

set function cannot be null

Error message

set function cannot be null

What it means

Companion to error 354: Localize throws InvalidOperationException when the set Action<VisualElement,string> is null, because it would have no way to write the localized string back onto the element.

Source

Thrown at Editor/Mono/Inspector/GraphicsSettingsInspectors/GraphicsSettingsInspectorUtility.cs:30

using UnityEditor.UIElements;
using UnityEditor.UIElements.ProjectSettings;
using UnityEngine;
using UnityEngine.Rendering;
using UnityEngine.UIElements;
using Unity.Scripting.LifecycleManagement;

namespace UnityEditor.Inspector.GraphicsSettingsInspectors
{
    public static partial class GraphicsSettingsInspectorUtility
    {
        #region Localization

        internal static void Localize(VisualElement visualElement, Func<VisualElement, string> get, Action<VisualElement, string> set)
        {
            if (get == null)
                throw new InvalidOperationException("get function cannot be null");
            if (set == null)
                throw new InvalidOperationException("set function cannot be null");

            var extractedText = get.Invoke(visualElement);
            if (string.IsNullOrWhiteSpace(extractedText))
                return;

            var localizedString = L10n.Tr(extractedText);
            set.Invoke(visualElement, localizedString);
        }

        internal static void LocalizeTooltip(VisualElement visualElement)
        {
            Localize(visualElement, e => e.tooltip, (e, s) => e.tooltip = s);
        }

        internal static void LocalizeText(Label visualElement)
        {
            Localize(visualElement, e => ((Label)e).text, (e, s) => ((Label)e).text = s);
        }

View on GitHub (pinned to 225b0fbdb5)

Solutions

  1. Always pass a non-null set lambda, e.g. (e, s) => e.text = s.
  2. Prefer the typed convenience methods (LocalizeTooltip etc.) that wire both lambdas.
  3. Null-check the delegate before calling if it is conditionally supplied.

Example fix

// before
Localize(elem, e => e.text, null); // throws

// after
Localize(elem, e => e.text, (e, s) => e.text = s);
Defensive patterns

Strategy: validation

Validate before calling

if (set == null) throw new ArgumentNullException(nameof(set));
GraphicsSettingsInspectorUtility.Localize(elem, get, set);

Prevention

When it happens

Trigger: Calling Localize(visualElement, get, null) — passing a null set delegate.

Common situations: Omitting the setter lambda when constructing a localize call. Misreading the parameter order and passing null for set.

Related errors


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