Creating a mesh in C#

Has anyone successfully created a mesh in C# script? I’ve searched the forums, unity wiki and google, finding lots of examples in JavaScript.

I get no errors and Unity acts like its creating a mesh as seen in the screen shot below, but in the scene view or game view window no mesh is shown. I’ve tried this with a JavaScript and it works just fine. Any ideas?

Here is the script.

using UnityEngine;
using System.Collections;

[RequireComponent(typeof(MeshFilter))]
[RequireComponent(typeof(MeshRenderer))]
public class FartScript : MonoBehaviour {

public GameObject theObj;

	// Use this for initialization
	void Start () {
		
		Mesh mesh = new Mesh();
		mesh.name = "testMesh";
		mesh.Clear();
		mesh.vertices = new Vector3[4];
		mesh.vertices[0].x = -10; mesh.vertices[0].y = 0; mesh.vertices[0].z = 0;
		mesh.vertices[1].x = 10; mesh.vertices[1].y = 0; mesh.vertices[1].z = 0;
		mesh.vertices[2].x = -10; mesh.vertices[2].y = 50; mesh.vertices[2].z = 0;
		mesh.vertices[3].x = 10; mesh.vertices[3].y = 50; mesh.vertices[3].z = 0;
		
		mesh.uv = new Vector2[4];
		mesh.uv[0].x = 0; mesh.uv[0].y = 0;
		mesh.uv[1].x = 1; mesh.uv[1].y = 0;
		mesh.uv[2].x = 0; mesh.uv[2].y = 1;
		mesh.uv[3].x = 1; mesh.uv[3].y = 1;

		mesh.triangles = new int[6];
		mesh.triangles[0] = 0;
		mesh.triangles[1] = 1;
		mesh.triangles[2] = 2;
		mesh.triangles[3] = 1;
		mesh.triangles[4] = 3;
		mesh.triangles[5] = 2;

		mesh.RecalculateNormals();
		
		MeshFilter mf = (MeshFilter)theObj.gameObject.GetComponent(typeof(MeshFilter));
		MeshRenderer mr = (MeshRenderer)theObj.gameObject.GetComponent(typeof(MeshRenderer));
		mf.mesh = mesh;
		mr.renderer.material.color = Color.white;
	}
	
	// Update is called once per frame
	void Update () {
	
	}
}

Thanks
V

1 Like

Yeah it works fine for me. I also used a Material reference to apply a better material to the mesh.

		_Mesh =
			new Mesh();
		
		_MeshFilter =
			(UnityEngine.MeshFilter)
				gameObject.AddComponent(typeof(MeshFilter));
		_MeshFilter.mesh = _Mesh;
		
		_MeshRenderer =
			(UnityEngine.MeshRenderer)
				gameObject.AddComponent(typeof(MeshRenderer));
		
		_MeshRenderer.materials =
			new Material[1];
		_MeshRenderer.materials[0] =
			_Material;
		_MeshRenderer.material = _Material;
		_MeshRenderer.castShadows = false;
		_MeshRenderer.receiveShadows = false;

I’m using AddComponent instead of GetComponent. I had some problems with GetComponent. That means you want to start with an empty game object.

Good luck pinching off that fart. The required component up there taught me something.

1 Like

The problem is that you’re not setting the arrays correctly. When creating a new mesh from scratch, you should create each array and store its reference in a local variable, fill it out with the data you want, and only then assign it to the mesh. For example:

Vector3[] vertices = new Vector3[4];

// No need to assign x, y and z separately!

vertices[0] = new Vector3(-10.0f, 0.0f, 0.0f);
vertices[1] = new Vector3(10.0f, 0.0f, 0.0f);
vertices[2] = new Vector3(-10.0f, 50.0f, 0.0f);
vertices[3] = new Vector3( 10.0f, 50.0f, 0.0f);

mesh.vertices = vertices;

This is necessary because when you assign an array to the mesh object, it makes an internal copy of the data. If you attempt to set its elements in place, you’ll only set it in a temporary copy of the array which will then be lost.

Similarly, if you want to edit an existing mesh, you should assign the value of mesh.vertices to a local variable (which will make a copy of the array for you), edit it, then assign it back to the mesh.

EDIT: just remembered, I wrote an example of mesh editing code in C# in my entry for the Explosivo contest, Leviathan Explosion. The source code is available on the wiki.

2 Likes

AAAHHH! :smile: Thanks thats exactly what it was! It’s clear now. :smile: … Very nice Explosion.

Works great now. :stuck_out_tongue:
Thanks
V

also an alternative a simple check of the components can be done and added if not they are not. This statement can check and add any component you can stack you add what you require, for that script to function correctly, like a prerequisite, both methods of forcing the component to be added when the script is applied to the gameobject

a simple if not statement.

if (!GetComponent())
gameObject.AddComponent();
if (!GetComponent())
gameObject.AddComponent();