Unity-Technologies/UnityCsReference · error · ArgumentNullException

triUV

Error message

triUV

What it means

Thrown by MeshUtility.SetPerTriangleUV2 when the triUV Vector2[] argument is null. The array must supply exactly 3 UVs per triangle, so null is rejected up front before triangle-count validation.

Source

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

        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);
        }

        public static Mesh.MeshDataArray AcquireReadOnlyMeshData(Mesh[] meshes)

View on GitHub (pinned to 225b0fbdb5)

Solutions

  1. Ensure the triUV array is allocated with length equal to 3 * triangleCount before calling.
  2. Treat a null from UV generation as an error and surface it earlier.
  3. Null-check triUV before invoking SetPerTriangleUV2.

Example fix

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

// after
var triUV = new Vector2[3 * triCount];
// ... fill triUV ...
MeshUtility.SetPerTriangleUV2(mesh, triUV);
Defensive patterns

Strategy: validation

Validate before calling

if (triUV == null) throw new ArgumentNullException(nameof(triUV));
if (triUV.Length != 3 * InternalMeshUtil.CalcTriangleCount(mesh)) return false;

Type guard

static bool IsValidTriUV(Vector2[] uvs, Mesh m) => uvs != null && m != null && uvs.Length == 3 * InternalMeshUtil.CalcTriangleCount(m);

Try / catch

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

Prevention

When it happens

Trigger: Calling SetPerTriangleUV2 with a null uv array — e.g. a UV-generation step that returned null on failure, or an unallocated array passed by mistake.

Common situations: Procedural UV tooling where the generation function returns null instead of an empty/valid array. A code path that allocates the array conditionally and skips it.

Related errors


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