Hi, new to Unity and scripting in general. I’ve been playing around with dynamically created meshes and am hitting a couple of problems.
Here is my code:
#pragma strict
@MenuItem("Custom/New Wall...")
static function addNewWall() {
var newVerts : Vector3[];
var uv : Vector2[];
var tris : int[] = [0,1,2,2,1,3];
var newWall : GameObject = new GameObject();
var newWallMesh : Mesh = new Mesh();
newVerts[0] = Vector3(2,0,0);
newVerts[1] = Vector3(0,3,0);
newVerts[2] = Vector3(-2,0,0);
newVerts[3] = Vector3(0,-3,0);
uv[0] = Vector2(0,1);
uv[1] = Vector2(1,1);
uv[2] = Vector2(0,0);
uv[3] = Vector2(0,1);
newWallMesh.vertices = newVerts;
newWallMesh.uv = uv;
newWallMesh.triangles = tris;
newWallMesh.RecalculateNormals ();
newWall.AddComponent(MeshFilter).mesh = newWallMesh;
newWall.AddComponent(MeshRenderer);
AssetDatabase.CreateAsset (newWallMesh, "Assets/NewWall.asset");
}
I’m being given a “NullReferenceException: Object reference not set to instance of object” error at line 14.
Is it saying that I’m trying to refer to newVerts[0] as if it’s a copy of an object called newVerts?
or is it something to do with my newVerts variable not being given a definite quantity of Vector3’s to contain?
The unity mesh documentation may need expanding to include adding some vertices to a newly created mesh.
The section about building from scratch assigns no actual vertices to the mesh.
Thanks in advance?