- Home /
creating a mesh from script
i have no idea why this code isn't working... its almost exactly what is placed in the scripting reference
using UnityEngine;
using System.Collections;
public class Window2 : MonoBehaviour {
public Vector3[] Vertices;
public Vector2[] UV;
public int[] Triangles;
void MeshSetup(){
Vector3[] Vertices = new Vector3[] {new Vector3(-1,0,1),new Vector3(1,0,1),new Vector3(1,0,-1),new Vector3(-1,0,-1)};
Vector2[] UV = new Vector2[] {new Vector2(0,256),new Vector2(256,256),new Vector2(256,0),new Vector2(0,0)};
int[] Triangles = new int[] {0,1,2,0,2,3};
}
void Start() {
MeshSetup();
Mesh stuff = new Mesh();
gameObject.AddComponent<MeshFilter>().mesh = stuff;
stuff.vertices = Vertices;
stuff.triangles = Triangles;
stuff.uv = UV;
}
}
so thats what i have, judging by everything i have read, that should work. right now i get 0 verts , 0 tris on the mesh that is added...
Answer by Fattie · Dec 27, 2012 at 10:54 AM
Dude you're made a trivial mistake
(purely at the language level, unrelated to mesh technology)
In lines 12 through 15 you're delcaring NEW local variables inside the routine MeshSetup !
wrong ...
Vector3[] Vertices = ...
Vector2[] UV = ...
int[] Triangles = ...
correct...
Vertices = ...
UV = ...
Triangles = ...
Hope it helps!
PS1: a good trick is, ALSO just add a mesh collider with the same mesh, very simply so you can seee it in the editor, evene if you have not yet added an actual texture to the visual mesh. So simply add the line
gameObject.AddComponent<MeshCollider>().mesh = stuff;
as the final line in Start(). TBC, note that of course you would not add collider mesh, at runtime in a game, only at startup time. Info here: http://answers.unity3d.com/questions/321428/adding-mesh-collider-in-run-time-slow.html
PS2: you will eventually need some normals. Creating normals is an art form, it is not something that can be done by the same software every time. Unity gives you for free a "rough and ready" normals routine which can be handy during development. it is called RecalculateNormals(). Understand that later you will have to write your own subtle normals-making software. Here is a discussion about that issue http://answers.unity3d.com/questions/329116/do-we-need-to-call-recalculatenormals-if-normal-ve.html
Hi Fattie,
Yes I agree, just as I said in my answer above :-)
That worked like a charm, thanks for the help. its sad but that had me for a couple days
Hey, I follow the corrected code and attach it to a Empty_Game_Object, and also add the Mesh_Filter component to Empty_Game_Object, then hit the play button, but then nothing shows up. So, is it OK that you show your code which is working again?
I am also wandering should I follow link text of Unity Reference/Documentation to build a mesh I want. Because somehow they don't explain well, or maybe it's just me don't quite understand it.
LOL for sure. I just meant "your problem here, was at the language level." ie, it has nothing to do with mesh, topology or what have you.
Essentially, we could perhaps call it a syntax error - although it's a little "higher level" than that. I don't know the best term for that exact type of error.
(Typical of the sad state of software engineering that all us guys don't have very specific words for these types of problems. All your buddies who are concrete engineers have incredibly specific terms for "we didn't put in enough water" "the pump got clogged" etc !)
Now I'll lose sleep for three days thinking of the best way to describe that particular type of program$$anonymous$$g error.
The program$$anonymous$$g error is called variable shadowing.
Answer by pako · Dec 27, 2012 at 10:49 AM
You are re-declaring the type of Vertices, UV, Triangle variables inside of the MeshSetup() method. As you have it now, they are treated as local variables, and their values are not assigned to the publicly declared variables of the same name, so, whatever is assigned to those values when the method runs are immediately forgoten when the method finishes.
This should work:
using UnityEngine;
using System.Collections;
public class Window2 : MonoBehaviour {
public Vector3[] Vertices;
public Vector2[] UV;
public int[] Triangles;
void MeshSetup(){
Vertices = new Vector3[] {new Vector3(-1,0,1),new Vector3(1,0,1),new Vector3(1,0,-1),new Vector3(-1,0,-1)};
UV = new Vector2[] {new Vector2(0,256),new Vector2(256,256),new Vector2(256,0),new Vector2(0,0)};
Triangles = new int[] {0,1,2,0,2,3};
}
void Start() {
MeshSetup();
Mesh stuff = new Mesh();
gameObject.AddComponent<MeshFilter>().mesh = stuff;
stuff.vertices = Vertices;
stuff.triangles = Triangles;
stuff.uv = UV;
}
}