# Creating a plane mesh directly from code

Hi

As in topic-I’d like to create a small Plane fully programatically.How to do that?
I think I have to create a new GameObject then add MeshFilter and MeshRenderer components to it.But what’s next?

you could use `gameObject.CreatePrimitive(PrimitiveType.Plane);`, but that will give you the ten by ten vertice plane we get from the gameObject Menu. I reccomend trying the below code. It will give you a four vertice and two tri plane.

``````var size : float;
function Awake()
{
var m : Mesh = new Mesh();
m.name = "Scripted_Plane_New_Mesh";
m.vertices = [Vector3(-size, -size, 0.01), Vector3(size, -size, 0.01), Vector3(size, size, 0.01), Vector3(-size, size, 0.01) ];
m.uv = [Vector2 (0, 0), Vector2 (0, 1), Vector2(1, 1), Vector2 (1, 0)];
m.triangles = [0, 1, 2, 0, 2, 3];
m.RecalculateNormals();
var obj : GameObject = new GameObject("New_Plane_Fom_Script", MeshRenderer, MeshFilter, MeshCollider);
obj.GetComponent(MeshFilter).mesh = m;
}
``````

See the CreatePlane script on the wiki.

The documentation on the Mesh functions is pretty thorough. Point 1 has a code example to build a mesh from scratch:

``````1. Building a mesh from scratch: should always be done in the following order: 1) assign vertices 2) assign triangles

var newVertices : Vector3[];
var newUV : Vector2[];
var newTriangles : int[];

function Start () {
var mesh : Mesh = new Mesh ();
GetComponent(MeshFilter).mesh = mesh;
mesh.vertices = newVertices;
mesh.uv = newUV;
mesh.triangles = newTriangles;
}
``````

GameObject plane = GameObject.CreatePrimitive(PrimitiveType.Plane);

``````// Position and rotation plane
plane.transform.position = new Vector3(1f, 2f, 3f);
``````