Skip to content

UNT0042 wrongly flags the setters too #520

Description

@MightyCreak

Bug description

Mesh array properties only allocates with the getters, not the setters, though the rule UNT0042 flags both as an error.

  • Version of analyzers assembly: 1.28.0
  • Analyzer rule: UNT0042
  • Error (exception message, type, and callstack where applicable): Mesh.vertices allocates a new array on each access. Cache it before the loop.

To Reproduce

Steps or code to reproduce the behavior:

[SerializeField]
private TMP_Text _tmpText;

protected void Update()
{
    var textInfo = _tmpText.textInfo;
    for (int i = 0; i < textInfo.meshInfo.Length; ++i)
    {
        var meshInfo = textInfo.meshInfo[i];
        meshInfo.mesh.vertices = meshInfo.vertices;  // <-- Throws a UNT0042 error
    }
}

Expected behavior

Only the getters should raise the issue.

Additional context

Claude verified in two ways that setters don't allocate:

  1. In Unity's Mesh.cs:
    • vertices setter calls SetArrayForChannel(VertexAttribute.Position, value)
    • Which wraps the array in a Span<byte> and calls SetSizedArrayForChannel
    • SetArrayForChannelImpl then pins the span with fixed and passes the pointer to native code, which copies it into the mesh. No managed array is created anywhere along the way
  2. By measuring the allocation count:
    • It ran 100 iterations on a 1000-vertex mesh and read Mono heap usage before and after:
      Calls Bytes allocated
      mesh.vertices = v; mesh.colors32 = c; 0
      mesh.SetVertices(v); mesh.SetColors(c); 0
      var x = mesh.vertices; var y = mesh.colors32; 1,638,400 (≈ 100 × (12 KB + 4 KB))

It used Profiler.GetMonoUsedSizeLong() to count the allocations.


To work around that: use the Set methods (e.g. SetVertices(), SetColors(), etc)

Activity

  1. changed the title [-]UNT0042 wrongly flag the setters too[/-] [+]UNT0042 wrongly flags the setters too[/+] on Nov 27, 2026
  2. sailro commented on Nov 30, 2026

    @sailro
    Member

    Thanks for the detailed report Creak (@MightyCreak), you're right.

    We double-checked it against Unity's reference source: the getters go through GetAllocArrayFromChannel (the native AllocExtractMeshComponentFromScript returns a new managed array), while the setters only wrap the array in a Span<byte> and pass it to SetMeshComponentFromSpanFromScript, so there is no managed allocation.

    This is fixed in #521:

    • assignments like mesh.vertices = ... are no longer reported (and the code fix no longer rewrites them),
    • accesses on a mesh that changes with each iteration (like meshInfo.mesh in your loop, or a foreach variable) are no longer reported either, as they cannot be cached before the loop.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

No labels
No labels

Type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions