Unity-Technologies/UnityCsReference · error · ArgumentNullException

Value cannot be null. (Parameter 'context')

Error message

Value cannot be null. (Parameter 'context')

What it means

Thrown by the public Profiler.GetOrCreateTrackerHandle(string trackerName, Type context) overload when the context Type parameter is null. The context type is used to map the performance marker to the assembly that owns it, enabling attribution in profiler tooling. The internal single-argument overload is allowed to pass null, but the public API contract requires a concrete Type.

Source

Thrown at Editor/Mono/Performance.bindings.cs:54

            if (m_Disposed)
                return;
            if (m_WatchHandle != -1)
                StopTracker(m_WatchHandle);
            m_Disposed = true;
        }

        internal const ulong InvalidTrackerHandle = ulong.MaxValue;
        private static extern ulong GetOrCreateTrackerHandleImpl(string trackerName, Type context);

        // This method is for internal use only. If you do not provide a type, we cannot
        // map the marker to an assembly.
        internal static ulong GetOrCreateTrackerHandle(string trackerName) => GetOrCreateTrackerHandleImpl(trackerName, null);

        public static ulong GetOrCreateTrackerHandle(string trackerName, Type context)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            return GetOrCreateTrackerHandleImpl(trackerName, context);
        }

        public static extern bool TryStartTrackerByHandle(ulong trackerHandle, out int trackerToken);

        public static extern string[] GetAvailableTrackers();
        public static extern bool Exists(string trackerName);
        public static extern void Reset(string trackerName);
        public static extern int GetSampleCount(string trackerName);
        public static extern double GetLastTime(string trackerName);
        public static extern double GetPeakTime(string trackerName);
        public static extern double GetAverageTime(string trackerName);
        public static extern double GetTotalTime(string trackerName);
        public static extern double GetTotalUsage(string trackerName);
        public static extern double GetTimestamp(string trackerName);
        public static extern void LogCallstack(string trackerName);

View on GitHub (pinned to 225b0fbdb5)

Solutions

  1. Pass a non-null Type as the context argument — typically typeof(YourClass) or the type that owns the code being profiled.
  2. If you obtained the Type via reflection, null-check it before calling GetOrCreateTrackerHandle.
  3. Use the internal single-argument overload if you are inside Unity's own assembly and do not need assembly attribution.

Example fix

// before
var handle = Profiler.GetOrCreateTrackerHandle("MyMarker", someTypeFromReflection);

// after
var handle = Profiler.GetOrCreateTrackerHandle("MyMarker", someTypeFromReflection ?? typeof(MyClass));
Defensive patterns

Strategy: validation

Validate before calling

Type contextType = resolvedType ?? typeof(MyProfilingClass);
var handle = Profiler.GetOrCreateTrackerHandle("MyMarker", contextType);

Prevention

When it happens

Trigger: Calling Profiler.GetOrCreateTrackerHandle(trackerName, null) — i.e. the public two-argument overload with a null Type. Commonly happens when reflecting over types at runtime and passing a possibly-null Type reference, or when copy-pasting from the internal overload signature.

Common situations: Writing custom performance profiling instrumentation; using reflection to obtain a Type that may be null; migrating from the internal single-argument overload to the public one and forgetting to supply the assembly-mapping context.

Related errors


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