Unity-Technologies/UnityCsReference · error · InvalidOperationException

get function cannot be null

Error message

get function cannot be null

What it means

GraphicsSettingsInspectorUtility.Localize takes a get delegate used to extract text from a VisualElement. A null get function throws InvalidOperationException immediately because the method cannot read the source text to localize.

Source

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

using UnityEditor.Rendering;
using UnityEditor.Rendering.Settings;
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)
        {

View on GitHub (pinned to 225b0fbdb5)

Solutions

  1. Always pass a non-null get lambda, e.g. e => e.text.
  2. Null-check the delegate before calling Localize if it may be absent.
  3. Use the convenience wrappers like LocalizeTooltip which supply the get/set lambdas for you.

Example fix

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

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

Strategy: validation

Validate before calling

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

Prevention

When it happens

Trigger: Calling Localize(visualElement, null, set) — passing a null Func<VisualElement,string> for the get parameter.

Common situations: Building a localize call dynamically and forgetting to supply the getter. Refactoring callers and dropping the lambda. Passing null where a lambda was expected due to a signature change.

Related errors


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