Procedural Mesh Generation question

I’ve been working on a procedural mesh generation system, and I have got a problem. Here is my code- and I have the problem that nothing is displayed in the scene or game view. I have an array of vertices. So what is wrong with my code?

var mesh : Mesh = new Mesh ();
var meshObject = Instantiate (empty, Vector3.zero, Quaternion.identity);
meshObject.gameObject.AddComponent("MeshFilter");
meshObject.gameObject.AddComponent("MeshRenderer");
GetComponent(MeshFilter).mesh = mesh;



mesh.Clear();
mesh.uv = newUV;

mesh.vertices = myVerts;
mesh.triangles = newTriangles;
newTriangles = [0,1,2];
var myUVs : Vector2[] = new Vector2[myVerts.Length];
for(var i = 0; i<myVerts.Length; i++)
{
	
    myUVs <em>= Vector2(myVerts_.x,myVerts*.y);*_</em>

}

}

First look and I’m not sure what your code is right (copy paste in different order perhaps). I think you want to set up the mesh after you’ve defined your variables, for instance switch these two lines around:

newTriangles = [0,1,2];
mesh.triangles = newTriangles;

Not sure if my following code will help you because its C#, but I made this a while ago when I was learning how to make my own mesh geometry.

using UnityEngine;
using System.Collections;

public class MakeGeometry : MonoBehaviour {

	// Use this for initialization
	void Start () {
    Mesh mesh = new Mesh();
    mesh.Clear();

    mesh.name = "IMadeThis";

    // create point data, [3] for 3 verticies of a simple triangle
    Vector3[] verts = new Vector3[3];  
    Vector3[] norms = new Vector3[3];
    Vector2[] uvs = new Vector2[3];
    int[] tris = new int[3];

    //create points for a triangle that is arrowhead like pointing at 0,0,0
    verts[0] = new Vector3(0, 0, 0);
    verts[1] = new Vector3(1, 1, 1);
    verts[2] = new Vector3(-1, 1, 1);
    //simple normals for now, they all just point up
    norms[0] = new Vector3(0, 1, 1);
    norms[1] = new Vector3(0, 1, 1);
    norms[2] = new Vector3(0, 1, 1);
    //simple UVs, traingle drawn in a texture
    uvs[0] = new Vector2(-1,0.5f);
    uvs[1] = new Vector2(-1,1);
    uvs[2] = new Vector2(1, 1);
    //create the triangle
    tris[0] = 0;
    tris[1] = 1;
    tris[2] = 2;
    
    //set the mesh up
    mesh.vertices = verts;
    mesh.normals = norms;
    mesh.uv = uvs;
    mesh.triangles = tris;

    //to get this mesh in the scene, we need an object
    GameObject myMesh = new GameObject("MyMesh");
    //add mesh fileter to this GameObject
    MeshFilter mf = myMesh.AddComponent<MeshFilter>();
    //set the mesh filter's mesh to our mesh
    mf.mesh = mesh;
    //to see it, we have to add a renderer
    myMesh.AddComponent<MeshRenderer>();
	}
	
}

Ok, so I thought it might be fun to try this out in JavaScript. Afterward I looked at your code and came to some possible conclusions.

What is the difference between

GetComponent(MeshFilter).mesh = mesh;

and

meshObject.GetComponent(MeshFilter).mesh = mesh;

My suspicion is that you’re attaching this script to a different Object or something, so the first line above from your code is being applied to that Object instead of the new one you instantiated.

Another potential problem is that I don’t see myVerts defined. I presume you are doing it elsewhere, but if you’re not setting them then your probably filling your mesh with zero size triangle(s).

Here’s my JS script that works like the CS script I posted before.

function Start ()
{
    var mesh : Mesh = new Mesh ();
    mesh.Clear();
  
    mesh.name = "IMadeThisJS";
  
	// create point data, [3] for 3 verticies of a simple triangle
    var verts = new Vector3[3] ;
    var norms = new Vector3[3] ;
    var uvs = new Vector2[3];
	var tris = new int[3];
  
    //create points for a triangle that is arrowhead like pointing at 0,0,0
    verts[0] = Vector3(0, 0, 0);
    verts[1] = Vector3(1, 1, 1);
    verts[2] = Vector3(-1, 1, 1);
    //simple normals for now, they all just point up
    norms[0] = Vector3(0, 1, 1);
    norms[1] = Vector3(0, 1, 1);
    norms[2] = Vector3(0, 1, 1);
    //simple UVs, traingle drawn in a texture
    uvs[0] = Vector2(-1, 0.5f);
    uvs[1] = Vector2(-1, 1);
    uvs[2] = Vector2(1, 1);
    //create the triangle
    tris[0] = 0;
    tris[1] = 1;
    tris[2] = 2;
	
	//set the mesh up
    mesh.vertices = verts ;
    mesh.normals = norms;
    mesh.uv = uvs;
	mesh.triangles = tris;
  
	//to get this mesh in the scene, we need an object
    var meshObject : GameObject = new GameObject("MyObjectMesh");
	//add mesh fileter to this GameObject
	meshObject.gameObject.AddComponent("MeshFilter");	
    //set the mesh filter's mesh to our mesh
	meshObject.GetComponent(MeshFilter).mesh = mesh;
	//to see it, we have to add a renderer
	meshObject.gameObject.AddComponent("MeshRenderer");
}