Unity-Technologies/UnityCsReference · error · ArgumentNullException

src

Error message

src

What it means

Thrown by MeshUtility.SetPerTriangleUV2 when the src Mesh argument is null. The method writes per-triangle secondary UVs (UV2) into the mesh, so a null mesh is rejected before the native SetPerTriangleUV2NoCheck call.

Source

Thrown at Editor/Mono/MeshUtility.bindings.cs:35

    {
        [FreeFunction] extern private static void OptimizeIndexBuffers([NotNull] Mesh mesh);
        [FreeFunction] extern private static void OptimizeReorderVertexBuffer([NotNull] Mesh mesh);

        public static void Optimize(Mesh mesh)
        {
            OptimizeIndexBuffers(mesh);
            OptimizeReorderVertexBuffer(mesh);
        }

        extern public static void SetMeshCompression([NotNull] Mesh mesh, ModelImporterMeshCompression compression);
        extern public static ModelImporterMeshCompression GetMeshCompression([NotNull] Mesh mesh);

        [NativeName("SetPerTriangleUV2")]
        static extern bool SetPerTriangleUV2NoCheck(Mesh src, Vector2[] triUV);
        public static bool SetPerTriangleUV2(Mesh src, Vector2[] triUV)
        {
            if (src == null)
                throw new ArgumentNullException("src");

            if (triUV == null)
                throw new ArgumentNullException("triUV");

            int triCount = InternalMeshUtil.CalcTriangleCount(src), uvCount  = triUV.Length;
            if (uvCount != 3 * triCount)
            {
                Debug.LogError("mesh contains " + triCount + " triangles but " + uvCount + " uvs are provided");
                return false;
            }
            return SetPerTriangleUV2NoCheck(src, triUV);
        }

        extern internal static Vector2[] ComputeTextureBoundingHull(Texture texture, int vertexCount);

        public static Mesh.MeshDataArray AcquireReadOnlyMeshData(Mesh mesh)
        {
            return new Mesh.MeshDataArray(mesh, false);

View on GitHub (pinned to 225b0fbdb5)

Solutions

  1. Null-check the mesh before calling and skip or report null entries.
  2. Ensure the Mesh field/asset is assigned and not destroyed before the call.
  3. In batch loops, filter out null meshes before processing.

Example fix

// before
MeshUtility.SetPerTriangleUV2(mesh, triUV);

// after
if (mesh == null) throw new ArgumentNullException(nameof(mesh));
MeshUtility.SetPerTriangleUV2(mesh, triUV);
Defensive patterns

Strategy: type-guard

Validate before calling

if (mesh == null) throw new ArgumentNullException(nameof(mesh));

Type guard

static bool IsValidMesh(Mesh m) => m != null;

Try / catch

try { MeshUtility.SetPerTriangleUV2(mesh, triUV); }
catch (ArgumentNullException ex) when (ex.ParamName == "src") { Debug.LogError("Mesh is null."); }

Prevention

When it happens

Trigger: Calling SetPerTriangleUV2(mesh, triUV) with a mesh reference that is null — an unassigned Mesh field, a mesh destroyed mid-operation, or a null returned by a GetComponent/asset lookup.

Common situations: Editor mesh-processing tooling operating on a MeshFilter.sharedMesh that is null. A batch processor that yields null for some entries in a mesh list.

Related errors


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