# Build plane on quad of mesh with same vertices

Hi!
I want to build a plane by code on a quad of a existing mesh. So to simply generate a plane i use this code:

``````using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class PlaneGenerator : MonoBehaviour
{
public List<Vector3> newVertices = new List<Vector3>();
public List<int> newTriangles = new List<int>();
public List<Vector2> newUV = new List<Vector2>();
private Mesh mesh;

void Start ()
{
mesh = GetComponent<MeshFilter> ().mesh;

float x = transform.position.x;
float y = transform.position.y;
float z = transform.position.z;

newVertices.Add( new Vector3 (x  , y  , z ));
newVertices.Add( new Vector3 (x + 1 , y  , z ));
newVertices.Add( new Vector3 (x + 1 , y-1  , z ));
newVertices.Add( new Vector3 (x  , y-1  , z ));

}

void Update ()
{
mesh.Clear ();
mesh.vertices = newVertices.ToArray();
mesh.triangles = newTriangles.ToArray();
mesh.uv = newUV.ToArray();
mesh.Optimize ();
mesh.RecalculateNormals ();
}
}
``````

The code obviously puts the vertices in the positions (0, 0, 0), (1, 0, 0), (1, -1, 0), (0, -1, 0). What I want is to generate the plane on a quad on a existing mesh. So the planes vertices will be at the position of the vertices of a random quad in the mesh. Therefore I think I need to get the vertices positions of two triangles (that build a quad thogether) of the mesh and put it’s positions for the positions of the vertices of the plane. How could I code this?

Thank you so much!

I thought I could find every triangle and then it’s vertices. I don’t know, if that’s even possible. I tried something like that, it was meant to build a triangle for each triangle of the mesh. It actually builds a triangle on the mesh, but only one:

``````using UnityEngine;
using System.Collections;

public class ExampleSpawn : MonoBehaviour
{
public GameObject spwanGO;
private Mesh spawnMesh;
public int[] spawnTris;
public Vector3[] spawnVecs;

void Start()
{
Mesh mesh = GetComponent<MeshFilter>().mesh;
mesh.Clear();

spawnMesh = spwanGO.GetComponent<MeshFilter>().mesh;
spawnTris = spawnMesh.triangles;
spawnVecs = spawnMesh.vertices;

for (int i = 0; i < spawnTris.Length; i += 3)
{
Vector3 p1 = spawnVecs[spawnTris[i + 0]];
Vector3 p2 = spawnVecs[spawnTris[i + 1]];
Vector3 p3 = spawnVecs[spawnTris[i + 2]];

mesh.vertices = new Vector3[] {transform.TransformPoint(p1), transform.TransformPoint(p2), transform.TransformPoint(p3)};
mesh.triangles = new int[] {i + 0, i + 1, i + 2};
}
}
}
``````