Add Plane to Mesh Filter

Hello everybody,
I am new in Unity. I am creating a plane by script. When my script starts in debug, it creates the plane, but the window Mesh Filter is empty. I need to add “Plane” here by script. Please, can you help me?
Thank you in advance guys!
Best regards.

2366988--160733--Schermata 2015-11-03 alle 17.01.30.png

Hi Andrea, Welcome to the forum!

You can use:

gameobject.GetComponent<MeshFilter>().mesh = myMesh.GetComponent<MeshFilter>().mesh;

Hope this helps!

I am sorry, but I am not understand. I am using this code to create programmatically the plane:

publicstaticvoidCreatePlane(floatwidth, floatheight){
Vector3TilePosition;
intnumberOfTiles = 16;
Texture_texture;
GameObjectgo = newGameObject (“Planez”);
MeshFiltermf = go.AddComponent (typeof(MeshFilter)) asMeshFilter;
MeshRenderermr = go.AddComponent (typeof(MeshRenderer)) asMeshRenderer;

Mesh m = newMesh ();
m.vertices = newVector3[ ]{
newVector3 (0, 0, 0),
newVector3 (width, 0, 0),
newVector3 (width, height, 0),
newVector3 (0, height, 0)
};

m.uv = newVector2[ ]{
newVector2 (0, 0),
newVector2 (0, 1),
newVector2 (1, 1),
newVector2 (1, 0),
};

m.triangles = newint[ ]{0,1,2,0,2,3};
mf.mesh = m;

m.RecalculateBounds ();
m.RecalculateNormals ();

}

Thank you for your welcome ad for your help :slight_smile:

Okay no worries, so:

GameObjectgo.GetComponent<MeshFilter> ().mesh = m;

Unfortunately it does not work. The mesh box is still empty. I attach picture of the result I need.
When I manually select “Plane”, I click on the circle at right of empty box, and the I select “Plane” of Assets panel. When I click just one time on Plane, down appears “Plane (Mesh) Library/Unity default resources”.
Thank you for your patience and for your help!

2368068--160811--Schermata 2015-11-04 alle 10.16.41.png

Hi Andrea, not a problem =)

I am a little confused however, your images show that the plane has been referenced in the “Mesh” section of the component, what am I missing?

Ok guys, I do not know Unity and I have the evidence! It is enough to change the construction of plane to GameObject go = GameObject.CreatePrimitive (PrimitiveType.Plane);
Thank you for your help and sorry if my question was not so clear!
Thank you a lot!

I’d like to point out, @AndreaMarinetti , that your original code DID work.

The problem is, because of the way you defined your normals, the plane was invisible from the direction you were looking at it from. If you rotated the camera around the new “Planez” object, you would have seen it.

This starts to get complicated, but the order you put the triangles determines the direction of the normal. In your case:

//m.triangles = new int[]{0,1,2,0,2,3};  <- old
m.triangles = new int[]{ 0,2,1, 0,3,2 }; // <- new

Would have faced the normals toward the camera.

I think I got you Andrea. It’s like the same problem I’m having right now, I wanted to create a new “Cube” game object but not from the editor but with a script, like from “scratch” entirely scripted. I wonder how is it possible and is it possible?

I know it’s your first post, but this is a 5 years old thread…
Hello btw. Please make a new thread with your question.