creating a mesh procidurally at runtime

i want to create a simple mesh procidurally but when i write this code nothing will be shown in gameview

how can i render a small and simple mesh? where is the problem? normals? colors? texture? uvs?

help me please!!! thank you very much

using UnityEngine;
using System.Collections;
public class album : MonoBehaviour
{
    public Texture2D tex;
    Mesh m;
    IEnumerator Start()
    {
        yield return new WaitForSeconds(1f);
        //print ("hello");
		m = new Mesh();
        m.vertices = new Vector3[5];
        m.uv = new Vector2[5];
        m.normals = new Vector3[5];
        m.normals[0] = new Vector3(0,0,-1); ;
        m.normals[1] = new Vector3(0, 0, -1);
		m.normals[2] = new Vector3(0, 0, -1);
		m.normals[3] = new Vector3(0, 0, -1);
		m.normals[4] = new Vector3(0, 1,0);
        m.vertices[0].x = 0;
        m.vertices[0].y = 0;
        m.vertices[0].z = 0;
        m.uv[0] = new Vector2 (0,0);
        m.vertices[1].x = 1;
        m.vertices[1].y = 0;
        m.vertices[1].z = 0;
        m.uv[1] = new Vector2(1, 0);
        m.vertices[2].x = 0;
        m.vertices[2].y = -1;
        m.vertices[2].z = 0;
        m.uv[2] = new Vector2(0, 1);
        m.vertices[3].x = 1;
        m.vertices[3].y = -1;
        m.vertices[3].z = 0;
        m.uv[3] = new Vector2(1, 1);
        m.vertices[4].x = 0;
        m.vertices[4].y = 0;
        m.vertices[4].z = 0.3f;
        m.uv[4] = new Vector2(1, 1);
        m.triangles = new int[9];
        m.triangles[0] = 0;
        m.triangles[1] = 1;
        m.triangles[2] = 2;
        m.triangles[3] = 2;
        m.triangles[4] = 3;
        m.triangles[5] = 1;
        m.triangles[6] = 1;
        m.triangles[7] = 0;
        m.triangles[8] = 4;
        m.RecalculateBounds();
        MeshFilter mf = (MeshFilter)transform.GetComponent(typeof(MeshFilter));
        mf.mesh = m;
        renderer.material = new Material(Shader.Find(" Diffuse"));
        mf.renderer.material.mainTexture = tex;
    }
}

[/code]

You can’t modify the property “x” , “y” and “z” but you have to do something like :

m.vertices[0] = new Vector3(0, 0, 0);
m.vertices[1] = new Vector3(1, 0, 0);

i don’t think that this is the problem.
i think this is a problem with normals or something like that
there is no error in my code and also there is no difference between setting vectors or their x,y,z components

by the way thank you very much but please copy my code to a script and then add the script to an object that has a mesh renderer like a cube and try to fix it to show something

thank you again my friend

If you can’t see the square in the editor, the most likely reason is that you are looking at it from the wrong side (it will be backface-culled). Try rotating the view and check if you can see it from the other side. Also, are you getting any error messages when you run this?

@andeee
it’s not visible from any side because it’s a rolling ball and i tried to view this from many sides
i don’t get any error message

can you test this for me

thank you

I will try it when i will be at work

Anyway, to check if problem is about your normals, just use :
m.RecalculateNormals();

And also try this :

m.triangles[0] = 0;
m.triangles[1] = 2;
m.triangles[2] = 3;
m.triangles[3] = 2;
m.triangles[4] = 1;
m.triangles[5] = 0;

If you see something in gameview then your problem is about your triangle list (triangles must be drawn in counterclockwise)

PS: Sorry for my bad english, not my native language

Actually, that is only true for a right-handed coordinate system (like the default for OpenGL). A left-handed system, like the one Unity uses, generally requires vertices in clockwise order.