- Home /
Unity isn't reading the mesh I created
In my game, I am proceduraly drawing a mesh. I know how to draw the mesh, so now Im putting it into a function so I can call the function later. Here is my code so far:
function Start(){
var block : GameObject = new GameObject("Block");
var mesh : Mesh = new Mesh ();
var meshFilter = block.AddComponent(MeshFilter);
block.AddComponent(MeshRenderer);
block.renderer.material = Resources.Load("VoxelMaterial");
DrawTheMesh();
}
function DrawTheMesh() {
var vertices = new Vector3[4];
var triangles = new int[6];
vertices[0] = Vector3(0,0,0);
vertices[1] = Vector3(0,0,1);
vertices[2] = Vector3(1,0,1);
vertices[3] = Vector3(1,0,0);
triangles = [0,1,2,0,2,3];
mesh.vertices = vertices;
mesh.triangles = triangles;
var uvs = new Vector2[mesh.vertices.length];
for (var i=0; i < uvs.Length; i+=1) {
uvs[i] = Vector2(mesh.vertices[i].x, mesh.vertices[i].z);
}
}
I know this code works just fine, because Ive drawn a shape with it before. But when I put it into a function called DrawTheMesh, it gives an error saying "unknown identifier: "mesh" ". Which doesn't make sense, since I clearly defined it at the very beggining during the Start function. Does anyone know why its doing this?
Answer by robertbu · Apr 12, 2013 at 01:02 AM
You are declaring 'mesh' inside Start() which means that mesh exists only during the lifetime of that specific Start() call, and only in the scope of the Start() function. Move the declaration to the top of the file (above the start function). You should be able to initialize it there as well.
That explains so much!! Thank you for saying that. Anyways, I now understand that if im going to need a variable to operate in many different functions, i need to declare them out of the start function to do so. Sadly, doing this caused more errors.
The errors say this:
"you are not allowed to call this function when declaring a variable"
"GameObject can only be called from the main thread"
I don't understand what the first error message means, and what is the main thread? how do I solve it?
It means that you need to execute the code inside Start() or some other function. So for your 'block' variable, you will put this at the top of the file:
var block : GameObject;
Then inside of Start() you will put:
block = new GameObject("Block");
Wow, can I just say I have learned a lot from your posts robertbu. I didn't know how to initiate the variable first, and then declare its value inside start() until now. And it all makes sense! I have only one error left, because all the other 6 error messages become solved.
For some reason, its sending an error that sais "Null reference exception". It sends me to line 28 of the code below:
internal var block : GameObject;
internal var mesh : $$anonymous$$esh;
internal var meshFilter;
function Start(){
DrawThe$$anonymous$$esh();
block = new GameObject("Block");
mesh = new $$anonymous$$esh ();
meshFilter = block.AddComponent($$anonymous$$eshFilter);
block.AddComponent($$anonymous$$eshRenderer);
block.renderer.material = Resources.Load("Voxel$$anonymous$$aterial");
}
function DrawThe$$anonymous$$esh() {
var vertices = new Vector3[4];
var triangles = new int[6];
vertices[0] = Vector3(0,0,0);
vertices[1] = Vector3(0,0,1);
vertices[2] = Vector3(1,0,1);
vertices[3] = Vector3(1,0,0);
triangles = [0,1,2,0,2,3];
mesh.vertices = vertices; //line 28
mesh.triangles = triangles;
var uvs = new Vector2[mesh.vertices.length];
for (var i=0; i < uvs.Length; i+=1) {
uvs[i] = Vector2(mesh.vertices[i].x, mesh.vertices[i].z);
}
}
I believe it thinks that object "mesh" that i made is a null reference, which is weird considering I assigned it. Any thoughts?
Because you are calling DrawThe$$anonymous$$esh();
in your Start function before you instantiate your mesh, so there is no mesh yet when you're trying to assign vertices to it. You need to call DrawThe$$anonymous$$esh();
after your mesh setup.
Also, you need to assign your mesh to your meshFilter as well.
meshFilter.mesh = mesh;
And might want to assign your uvs to your mesh at the end...
mesh.uv = uvs;
Your answer
Follow this Question
Related Questions
Landscape has a awry vertex 1 Answer
Why isn't this mesh not appearing properly? 0 Answers
creating a mesh procedurally 4 Answers
Easy way to convert a bunch of vertices to triangles or uv's? 1 Answer
minimize vertex artifacting for procedural mesh manipulations - vertex collision same object? 0 Answers