Hi there. Just playing around with mesh generation and have thrown this script together that allows me to make a plane based on the width and height I enter. However I can’t get the UV to work as i get this error
Mesh.uv is out of bounds. The supplied array needs to be the same size as the Mesh.vertices array.
UnityEngine.Mesh:set_uv(Vector2[])
createPlane:Start() (at Assets/Scripts/createPlane.js:43)
I am very confused by it as the uv array is the same size as the vertices one, as proved by my code which prints the length of each array. Here is the code in javascript.
var width : float;
var height : float;
var tex : Material;
function Start()
{
var m : Mesh = new Mesh();
m.name = "mesh1";
var vertices = new Vector3[width*height];
var uv = new Vector2[width*height];
var tris = new int[(width-1)*(height-1)*6];
var i=0;
for(var x = 0;x<width;x++){
for(var y = 0;y<height;y++){
vertices *= (Vector3(x,0,y));*
_ print(vertices*);_
_ uv = (Vector2(x,y));
print(uv);
i++;
}
}*_
var t : int = 0;
var index : int ;
for(var x2 : int = 0; x2 < width - 1; x2++)
{
for(var y2 : int = 0; y2 < height - 1; y2++)
{
index = x2 * height + y2;
tris[t++] = index;
tris[t++] = index + 1;
tris[t++] = index + 1 + height;
tris[t++] = index;
tris[t++] = index + 1 + height;
tris[t++] = index + height;
}
}
* print(“uv array is “+uv.Length+” items long”);*
* print(“verticies array is “+vertices.Length+” items long”);*
* m.uv = uv;*
* m.vertices = vertices;*
* m.triangles = tris;*
* m.RecalculateNormals();*
* var obj : GameObject = new GameObject(“plane1”, MeshRenderer, MeshFilter, MeshCollider);*
* obj.GetComponent(MeshFilter).mesh = m;*
* obj.GetComponent(MeshCollider).sharedMesh = m;*
}
Thank you for any help