Modifying a Mesh at Runtime

I have read the documentation, and used their code (Unity - Scripting API: Mesh).

I have an empty game object with a mesh filter and a mesh renderer. When I run it, I get a pink square.

I have attached this script to this game object:

public Vector3[] newVertices;
public Vector2[] newUV;
public int[] newTriangles;
void Update() {
    Mesh mesh = GetComponent<MeshFilter>().mesh;
    mesh.Clear();
    mesh.vertices = newVertices;
    mesh.uv = newUV;
    mesh.triangles = newTriangles;
}

When I use these settings, my pink square vanishes (instead of changing):

31469-mesh.png

I get the warning "Shader wants texture coordinates, but the mesh Quad Instance doesn’t have them. I have tried with other meshes, and it is still the same error. (No mesh just draws nothing from the beginning.)

The code is pulled directly from the documentation. Where am I going wrong?

Any mesh shall have set of vertices and a set of texture coordinates(UV), and, of cpuse, triangles. That, set array of UV(newUV), for your case this array contains elements(Vector2):

 (0, 0), (1, 0), (0, 1)

Or see my simple script below(for GameObject with only Transform component):

 public Material myMat = null;
 void Start() {
  gameObject.AddComponent("MeshFilter");
  gameObject.AddComponent("MeshRenderer");
  Mesh mesh = GetComponent<MeshFilter>().mesh;
  mesh.Clear();
  mesh.vertices = new Vector3[] {new Vector3(0, 0, 0), new Vector3(0, 1, 0), new Vector3(1, 1, 0)};
  mesh.uv = new Vector2[] {new Vector2(0, 0), new Vector2(0, 1), new Vector2(1, 1)};
  mesh.triangles = new int[] {0, 1, 2};
  this.renderer.material = myMat; //apply material for mesh
 }

I hope that it will help you.