Create a dynamic mesh (cube)

Hi everybody I’am new on the forum and is my first week with Unity (6 years Shockwave 3D)
I’am trying to create a dynamic mesh a start with a simple cube

here is my code and a put the script on the main Camera

i saw nothing on the scene and no error message.

what I’am doing wrong

function makeCube () {


var size = 1500;

var VertexList = [
	Vector3(-size, -size,  -size),
	Vector3(-size,  size,  -size),
	Vector3( size,  size,  -size),
	Vector3( size, -size,  -size),
	Vector3( size, -size,   size),
	Vector3( size,  size,   size),
	Vector3(-size,   size,  size),
	Vector3(-size, -size,  size)
	];

var FaceList = [
	0, 1, 3, //   1: face arrière
	0, 2, 3,
	3, 2, 5, //   2: face droite
	3, 5, 4, 
	5, 2, 1, //   3: face dessue
	5, 1, 6,
	3, 4, 7, //   4: face dessous
	3, 7, 0, 
	0, 7, 6, //   5: face gauche
	0, 6, 1,
	4, 5, 6, //   6: face avant
	4, 6, 7
	];
	
	


	var newUVs = [
	Vector2(0,0), //   1: face arrière
	Vector2(0,0),
	Vector2(0,0), //   2: face droite
	Vector2(0,0), 
	Vector2(0,0), //   3: face dessue
	Vector2(0,0),
	Vector2(0,0), //   4: face dessous
	Vector2(0,0), 
	Vector2(0,0), //   5: face gauche
	Vector2(0,0),
	Vector2(0,0), //   6: face avant
	Vector2(0,0)
	];
	
	var mesh = new Mesh ();

	meshFilter = GetComponent(typeof(MeshFilter)) as MeshFilter;

	mesh.vertices = VertexList;
	
	//mesh.uv = newUVs;
	mesh.triangles = FaceList;
   
	mesh.RecalculateNormals();
	
	//meshFilter.transform.position = Vector3(0, 0.5, 0);

}

if somebody a script and little explanation to do this kind of stuff to help me to understand how to do a dynamic mesh

Sorry for my poor english

cheers,
Alain

I assume you’re actually calling the makeCube function from somewhere. I also assume that the object the script is attached to has a mesh filter and a mesh renderer. If not, you need to fix those things.

Also,

	meshFilter = GetComponent(typeof(MeshFilter)) as MeshFilter;

doesn’t do anything. You have to assign the mesh you’re creating to the mesh filter:

	GetComponent(MeshFilter).mesh = mesh;

–Eric

Welcome! :smile:

There are a few things in that script that prevent it from working as-is.
Edit: Which Eric already touched upon! 8)

  • As written, the function never gets called. That might be obvious and you might know about it, but I put in a Start() function anyway to make sure.

  • You may not have a MeshRenderer attached to the Game Object. I made the script require it at the bottom.

  • You never actually assigned the mesh to the MeshFilter.
    Another thing that won’t stop it from working, but is good practice, is to put your variables (size) outside of functions, so you can change them in the Editor.

Also, in Unity, the variables that you have capitalized (VertexList, FaceList), by convention, are not.

var size = 1500;

function Start () {makeCube();}

function makeCube () {	
	var vertexList = [ 
		Vector3(-size, -size, -size), 
		Vector3(-size,  size, -size), 
		Vector3( size,  size, -size), 
		Vector3( size, -size, -size), 
		Vector3( size, -size,  size), 
		Vector3( size,  size,  size), 
		Vector3(-size,  size,  size), 
		Vector3(-size, -size,  size) 
	]; 
	
	var faceList = [ 
		0, 1, 3, //   1: face arrière 
		0, 2, 3, 
		3, 2, 5, //   2: face droite 
		3, 5, 4, 
		5, 2, 1, //   3: face dessue 
		5, 1, 6, 
		3, 4, 7, //   4: face dessous 
		3, 7, 0, 
		0, 7, 6, //   5: face gauche 
		0, 6, 1, 
		4, 5, 6, //   6: face avant 
		4, 6, 7 
	]; 
	    	
	var mesh = new Mesh(); 
	mesh.vertices = vertexList; 
	mesh.triangles = faceList;
	mesh.RecalculateNormals();
	
	(GetComponent(MeshFilter) as MeshFilter).mesh = mesh;	    
}

@script RequireComponent(MeshFilter)
@script RequireComponent(MeshRenderer)

Thank for help

Now I can see the cube. :stuck_out_tongue:

I try to add the textureCoord but with this code is not good i have this error message (textureCoord is not a member of “UnityEngine.MEsh”)

var size = 1;

function Start () {makeCube();}

function makeCube () {   
   var vertexList = [
      Vector3(-size, -size, -size),
      Vector3(-size,  size, -size),
      Vector3( size,  size, -size),
      Vector3( size, -size, -size),
      Vector3( size, -size,  size),
      Vector3( size,  size,  size),
      Vector3(-size,  size,  size),
      Vector3(-size, -size,  size)
   ];
   
	var faceList = [
      0, 1, 3, //   1: face arrière
      0, 2, 3,
      3, 2, 5, //   2: face droite
      3, 5, 4,
      5, 2, 1, //   3: face dessue
      5, 1, 6,
      3, 4, 7, //   4: face dessous
      3, 7, 0,
      0, 7, 6, //   5: face gauche
      0, 6, 1,
      4, 5, 6, //   6: face avant
      4, 6, 7
   ];
      
	 var textureCoordinate= [ 
	Vector2(0.0,  0.0), 
	Vector2(1.0,  0.0), 
	Vector2(0.0,  1.0), 
	Vector2(1.0,  0.0)
	];
	

   var mesh = new Mesh();
   mesh.vertices = vertexList;
   mesh.triangles = faceList;
   
   mesh.textureCoord = textureCoordinate;
   mesh.RecalculateNormals();
   //mesh.transform.position = Vector3(0, 0.5, 0)
   //mesh.position = Vector3(0, 0.5, 0)
   (GetComponent(MeshFilter) as MeshFilter).mesh = mesh;   
	

}

@script RequireComponent(MeshFilter)
@script RequireComponent(MeshRenderer)

Probably is in the Component(MeshRenderer) but how to write the code in the correct format.

How to place the cube in the scene i try this and is doesn’t work
//mesh.transform.position = Vector3(0, 0.5, 0)
//mesh.position = Vector3(0, 0.5, 0)

cheers,
Alain

It’s “uv” rather than “textureCoord”. However, that still won’t work, because you need to have the same number of items in the array for uvs (and other things like normals etc.) as you have for the vertices. I’d recommend reading the docs on the Mesh class.

For moving the cube, you need to set the object’s transform.

–Eric

   1.
      var size = 1;
   2.
       
   3.
      function Start () {makeCube();}
   4.
       
   5.
      function makeCube () {  
   6.
         var vertexList = [
   7.
            Vector3(-size, -size, -size),
   8.
            Vector3(-size,  size, -size),
   9.
            Vector3( size,  size, -size),
  10.
            Vector3( size, -size, -size),
  11.
            Vector3( size, -size,  size),
  12.
            Vector3( size,  size,  size),
  13.
            Vector3(-size,  size,  size),
  14.
            Vector3(-size, -size,  size)
  15.
         ];
  16.
         
  17.
          var faceList = [
  18.
            0, 1, 3, //   1: face arrière
  19.
            0, 2, 3,
  20.
            3, 2, 5, //   2: face droite
  21.
            3, 5, 4,
  22.
            5, 2, 1, //   3: face dessue
  23.
            5, 1, 6,
  24.
            3, 4, 7, //   4: face dessous
  25.
            3, 7, 0,
  26.
            0, 7, 6, //   5: face gauche
  27.
            0, 6, 1,
  28.
            4, 5, 6, //   6: face avant
  29.
            4, 6, 7
  30.
         ];
  31.
           
  32.
           var textureCoordinate= [
  33.
          Vector2(0.0,  0.0),
  34.
          Vector2(1.0,  0.0),
  35.
          Vector2(0.0,  1.0),
  36.
          Vector2(1.0,  0.0)
  37.
          ];
  38.
         
  39.
       
  40.
         var mesh = new Mesh();
  41.
         mesh.vertices = vertexList;
  42.
         mesh.triangles = faceList;
  43.
         
  44.
         mesh.uv = textureCoordinate;
  45.
         mesh.RecalculateNormals();
  46.
         renderer.material.mainTexture = Resources.Load("file_texture_name_at_folder");
  47.
        renderer.material.color = Color.red; //for example
  48.
        
  49.
         
  50.
       
  51.
      }
  52.
       
  53.
      @script RequireComponent(MeshFilter)
  54.
      @script RequireComponent(MeshRenderer)

Should the first three elements in faceList be 0, 1, 2 ?

You’re about a two years too late man :lol: