title: Unity - How to set the color and UV map of an individual face when clicking a mesh?
body: <p>i saw <a href="https://stackoverflow.com/questions/57423218/unity-how-to-set-the-color-of-an-individual-face-when-clicking-a-mesh?rq=1">Unity - How to set the color of an individual face when clicking a mesh?</a> this Question and I'm challenging this. However, I have no understanding of how to reconstruct UV. If there's anyone who can tell me which part to modify in the code below, I'd like to get help.</p>
  <pre><code>using System.Collections;
  using System.Collections.Generic;
  using UnityEngine;
  using System.Linq;
  public class meshColor : MonoBehaviour
  {
      public GameObject cube;
  
      [SerializeField] private MeshFilter meshFilter;
      [SerializeField] private MeshRenderer meshRenderer;
      [SerializeField] private MeshCollider meshCollider;
  
      private Mesh _mesh;
  
      private void Awake()
      {
          if (!meshFilter) meshFilter = GetComponent&lt;MeshFilter&gt;();
          if (!meshRenderer) meshRenderer = GetComponent&lt;MeshRenderer&gt;();
          if (!meshCollider) meshCollider = GetComponent&lt;MeshCollider&gt;();
  
          _mesh = meshFilter.mesh;
  
          var colors = new Color[_mesh.vertices.Length];
          for (var k = 0; k &lt; colors.Length; k++)
          {
              colors[k] = Color.white;
          }
  
          _mesh.colors = colors;
      }
  
      private void Update()
      {
          if (!Input.GetMouseButtonDown(0)) return;
  
          var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
  
          if (Physics.Raycast(ray, out var hit))
          {
              Debug.Log(hit.triangleIndex);
                  
              var vertices = _mesh.vertices;
              var uvs = _mesh.uv;
              var triangles = _mesh.triangles;
              var colors = _mesh.colors;
                      
              var vert1Index = triangles[hit.triangleIndex * 3 + 0];
              var vert2Index = triangles[hit.triangleIndex * 3 + 1];
              var vert3Index = triangles[hit.triangleIndex * 3 + 2];
                      
              var vert1Pos = vertices[vert1Index];
              var vert2Pos = vertices[vert2Index];
              var vert3Pos = vertices[vert3Index];
  
              var uv1Pos = uvs[vert1Index];
              var uv2Pos = uvs[vert2Index];
              var uv3Pos = uvs[vert3Index];
  
              var vert1Occurrences = 0;
              var vert2Occurrences = 0;
              var vert3Occurrences = 0;
              foreach (var index in triangles)
              {
                  if (index == vert1Index) vert1Occurrences++;
                  else if (index == vert2Index) vert2Occurrences++;
                  else if (index == vert3Index) vert3Occurrences++;
              }
          
              var newVertices = vertices.ToList();
              var newColors = colors.ToList();
              var newUvs = uvs.ToList();
          
              if (vert1Occurrences &gt; 1)
              {
                  newVertices.Add(vert1Pos);
                  newColors.Add(new Color());
                  newUvs.Add(uv1Pos);
                  vert1Index = newVertices.Count - 1;
              }
  
              if (vert2Occurrences &gt; 1)
              {
                  newVertices.Add(vert2Pos);
                  newColors.Add(new Color());
                  newUvs.Add(uv2Pos);
                  vert2Index = newVertices.Count - 1;
              }
  
              if (vert3Occurrences &gt; 1)
              {
                  newVertices.Add(vert3Pos);
                  newColors.Add(new Color());
                  newUvs.Add(uv3Pos);
                  vert3Index = newVertices.Count - 1;
              }
          
              triangles[hit.triangleIndex * 3 + 0] = vert1Index;
              triangles[hit.triangleIndex * 3 + 1] = vert2Index;
              triangles[hit.triangleIndex * 3 + 2] = vert3Index;
  
              newColors[vert1Index] = Color.red;
              newColors[vert2Index] = Color.red;
              newColors[vert3Index] = Color.red;
  
              newUvs[vert1Index] = new Vector2(uv1Pos.x, uv1Pos.y);
              newUvs[vert2Index] = new Vector2(uv2Pos.x, uv2Pos.y);
              newUvs[vert3Index] = new Vector2(uv3Pos.x, uv3Pos.y);
  
              _mesh.vertices = newVertices.ToArray();
              _mesh.triangles = triangles;
              _mesh.colors = newColors.ToArray();
              _mesh.uv = newUvs.ToArray();
  
              _mesh.RecalculateNormals();
          }
          else
          {
              Debug.Log(&quot;no hit&quot;);
          }
      }
  }
  </code></pre>